My question is sizeof(char) is 1 byte but while executing below code why I am getting wrong output. Kindly help me. Thank you
typedef struct {
int x;
int y;
char a;
}Point2D;
main() {
Point2D *myPoint=malloc(sizeof(Point2D));
NSLog(@"sizeof(Point2D): %zu", sizeof(Point2D));
}
Output: sizeof(Point2D) : 12 //But it should return 9 [int + int + char / 4 + 4 + 1]
Note: While running char individually , I am getting correct output
Example:
typedef struct {
char a;
char b;
}Point2D;
main() {
Point2D *myPoint=malloc(sizeof(Point2D));
NSLog(@"sizeof(Point2D): %zu", sizeof(char));
}
output: sizeof(char) : 2