I modified your program slightly to get it to work:
#include<stdio.h>
struct a {
int b1;
int c1;
char d1;
};
int main() {
unsigned int b=10;
unsigned int c;
c = b - (unsigned int )sizeof(struct a);
fprintf(stdout,"size of a: %d, size of int: %d, size of char: %d\n",sizeof(struct a),sizeof(int),sizeof(char));
fprintf(stdout,"%d\n",c);
return(0);
}
This is the output:
# ./a.out
size of a: 12, size of int: 4, size of char: 1
-2
Now I agree that for two ints and one char, the size should be 4+4+1=9, but memory allocation is not done a single byte at a time. This is system dependent.
Hi ,
the solution given above is right but i want to correct at one point.
c = b - (unsigned int )sizeof(struct a);
printf("%d\n",c); will print -2 because any struct occupy the memory in chunk of words. and for linux word is 4 bytes. so for char it occupies 4 bytes give one byte to char and pad the rest 3 bytes so sizeof(struct a) gives 12 as size