abi_a
asked on
sizeof() operator for a structure
there r 2 ques:
1.what will be the size of an empty structure?
structure test
{
};
main()
{
struct test *p;
p = (struct test *)malloc(sizeof(struct test));
printf("%d",sizeof(struct test));
}
It prints 1.But wat does that mean.
2.and if i print like printf("%d"); gives me -1? why is this so.
Where can i get answers for questions like this.
thanks
1.what will be the size of an empty structure?
structure test
{
};
main()
{
struct test *p;
p = (struct test *)malloc(sizeof(struct test));
printf("%d",sizeof(struct test));
}
It prints 1.But wat does that mean.
2.and if i print like printf("%d"); gives me -1? why is this so.
Where can i get answers for questions like this.
thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Happens in EE all the time, I'm used to it :) Sometimes good, somewhat reassuring to the asker.
Sjef
Sjef
struct Data {
} ;
int main () {
printf ( "%d", sizeof ( struct Data ) ) l
}
would print 1....because of the following
Consider before printf, u declare an array of the struct Data
struct Data arr[10] ;
As u know, array elemnets are stored in contiguous memrory locations...so each elemnt should have a distinct address
Thus in our case, if the size would have been 0, then each elemnet would not have had a unique addr
Hence a non zero size
Amit
} ;
int main () {
printf ( "%d", sizeof ( struct Data ) ) l
}
would print 1....because of the following
Consider before printf, u declare an array of the struct Data
struct Data arr[10] ;
As u know, array elemnets are stored in contiguous memrory locations...so each elemnt should have a distinct address
Thus in our case, if the size would have been 0, then each elemnet would not have had a unique addr
Hence a non zero size
Amit
#include <stdio.h>
struct Data {
} ;
int main () {
printf ( "%d", sizeof ( struct Data ) ) ;
}
=========
the above prints 0 on my machine. ( linux /gcc)
struct Data {
} ;
int main () {
printf ( "%d", sizeof ( struct Data ) ) ;
}
=========
the above prints 0 on my machine. ( linux /gcc)
I even modified the code to
struct Data {
} data;
int main () {
printf ( "%d\n", sizeof ( struct Data ) );
printf ( "%d\n", sizeof ( data ) );
}
and Linux prints two lines with zeroes... The outcome is not defined in the C language, and hence compiler-dependent.
struct Data {
} data;
int main () {
printf ( "%d\n", sizeof ( struct Data ) );
printf ( "%d\n", sizeof ( data ) );
}
and Linux prints two lines with zeroes... The outcome is not defined in the C language, and hence compiler-dependent.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh.....Sorry Guys........I didn't notice the TA...I thought I was answering C++ question
Amit
Amit
I didn't see your answer