confused in sizeof structures

R

rahul8143

hello,
Why am i getting different sizeof values for struct mm and mm1?
Also why name array of length upto 4 gives struct sizes as 8 and above
that changes to 12?

#include <stdio.h>
main()
{

struct mm
{
char *name;
int no;
};

struct mm1
{
char *name[0];
int no;
};

struct mm2
{
char name[4];
int no;
};
struct mm3
{
char name[5];
int no;
};
printf("size of mm is =%d\n",sizeof(struct mm));
printf("size of mm1 is =%d\n",sizeof(struct mm1));
printf("size of mm2 is =%d\n",sizeof(struct mm2));
printf("size of mm3 is =%d\n",sizeof(struct mm3));
}
/*output
size of mm is =8
size of mm1 is =4
size of mm2 is =8
size of mm3 is =12 */
 
L

lndresnick

hello,
Why am i getting different sizeof values for struct mm and mm1?
Also why name array of length upto 4 gives struct sizes as 8 and above
that changes to 12?

#include <stdio.h>
main()
{

struct mm
{
char *name;
int no;
};

struct mm1
{
char *name[0];
int no;
};

struct mm2
{
char name[4];
int no;
};
struct mm3
{
char name[5];
int no;
};
printf("size of mm is =%d\n",sizeof(struct mm));
printf("size of mm1 is =%d\n",sizeof(struct mm1));
printf("size of mm2 is =%d\n",sizeof(struct mm2));
printf("size of mm3 is =%d\n",sizeof(struct mm3));
}
/*output
size of mm is =8
size of mm1 is =4
size of mm2 is =8
size of mm3 is =12 */

For mm vs mm1, it violates a constraint to have the size of an
array as 0 (6.7.5.2 of the standard says of the content of [] "If the
expression is a constant expression, it shall have a value greater than
zero.".
The compiler I use gripes about this with warnings enabled ("warning:
ISO C forbids zero-size array `foo'"), you might consider invoking your
compiler differently if yours does not.

For mm2 vs mm3, see the FAQ at
http://www.eskimo.com/~scs/C-faq/q2.13.html.

-David
 
D

Darius

hello,
Why am i getting different sizeof values for struct mm and mm1?
Also why name array of length upto 4 gives struct sizes as 8 and above
that changes to 12?

#include <stdio.h>
main()
{

struct mm
{
char *name;
int no;
};
char * is 4 bytes + in is 4 bytes = 8 bytes
struct mm1
{
char *name[0];
int no;
};
an array of size 0 takes 0 bytes + int is 4 bytes = 4 bytes
struct mm2
{
char name[4];
int no;
};
array of 4 chars take 4 bytes + int takes 4 bytes = 8 bytes
struct mm3
{
char name[5];
int no;
};
array of 5 chars 5 bytes + int 4 bytes + 3 bytes of padding(to pad to
word boundry) yes it pads to word boundary. = 12 bytes
 
E

Emmanuel Delahaye

hello,
Why am i getting different sizeof values for struct mm and mm1?
Also why name array of length upto 4 gives struct sizes as 8 and above
that changes to 12?

#include <stdio.h>
main()
{

struct mm
{
char *name;
int no;
};

struct mm1
{
char *name[0];

[C90] you meant :

char *name[1];
int no;
};

struct mm2
{
char name[4];
int no;
};
struct mm3
{
char name[5];
int no;
};
printf("size of mm is =%d\n",sizeof(struct mm));
printf("size of mm1 is =%d\n",sizeof(struct mm1));
printf("size of mm2 is =%d\n",sizeof(struct mm2));
printf("size of mm3 is =%d\n",sizeof(struct mm3));
}
/*output
size of mm is =8
size of mm1 is =4
size of mm2 is =8
size of mm3 is =12 */

Computer Science uses fuzzy logic...

Borland C++ 3.1 (Large) :

size of mm is =6
size of mm1 is =6
size of mm2 is =6
size of mm3 is =7

Borland C++ 3.1 (Small) :

size of mm is =4
size of mm1 is =4
size of mm2 is =6
size of mm3 is =7

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top