mercredi 8 septembre 2021

What should be the output of the program-1 and program-2? why char increases the size of the c++ class? [duplicate]

Program-1

 #include<iostream>
 using namespace std;
 class test
 {
     char name;
     int data;
 };
 int main()
 {
     cout<<sizeof(test);
     return 0;
 }

Program-2

#include<iostream>
using namespace std;
class test
{
    char *name;
    int data;
};
int main()
{
   cout<<sizeof(test);
   return 0;
}

Why char increase the size of class even though its size is 1 byte. plz explain with example.

Aucun commentaire:

Enregistrer un commentaire