By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

structure

P: 23
i have different structure definitions.could any one tell me why it is not taking the folowing variations

Expand|Select|Wrap|Line Numbers
  1. ex1:
  2. #include<stdio.h>
  3.  
  4. struct{
  5. int i;              // 4 bytes
  6. char c;          // 1 byte
  7. char b;          // 1 byte
  8. };
  9.  
  10. main()
  11.  {
  12. struct a p;
  13.  
  14.  
  15.  
  16.   printf("%d",sizeof(p));
  17. }
gcc produces the given error :storage size of `p' isn't known

ex2.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. struct {
  4. int i;              // 4 bytes
  5. char c;          // 1 byte
  6. char b;          // 1 byte
  7. }a;
  8.  
  9. main()
  10.  {
  11. struct a p;
  12.  
  13.  
  14.  
  15.   printf("%d",sizeof(p));
  16. }
gcc produces "storage size of `p' isn't known"

could any one tell me why this above are wrong?
Oct 3 '06 #1
Share this Question
Share on Google+
5 Replies


100+
P: 144
You are using sizeof wrong. It expects a type to be passed to it, but you are passing an actual variable.

Expand|Select|Wrap|Line Numbers
  1. struct a p;
  2.  
  3. // Wrong
  4. sizeof(p);
  5.  
  6. // Correct
  7. sizeof(struct a);
  8.  
Oct 3 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
You are using sizeof wrong. It expects a type to be passed to it, but you are passing an actual variable.

Expand|Select|Wrap|Line Numbers
  1. struct a p;
  2.  
  3. // Wrong
  4. sizeof(p);
  5.  
  6. // Correct
  7. sizeof(struct a);
  8.  
No this is wrong sizeof can take a type or a variable and most people suggest using the variable name because then if the type changes the code stays correct with any further changes.

The problem lies in the way the structure is declared

Expand|Select|Wrap|Line Numbers
  1. struct {
  2.   int i;              // 4 bytes
  3.   char c;          // 1 byte
  4.   char b;          // 1 byte
  5. }a;
  6.  
  7. struct a p;
  8.  
This does not define a type of structure named a, it defines a variable a that is a structure you need

Expand|Select|Wrap|Line Numbers
  1. struct a {
  2.   int i;              // 4 bytes
  3.   char c;          // 1 byte
  4.   char b;          // 1 byte
  5. };
  6.  
  7. struct a p;
  8.  
This defines a type of structure named a which can then be used to define a variable p of that type.


P.S.

main()

should be

int main()

to avoid undefined behaviour
Oct 3 '06 #3

100+
P: 144
Banfa, you are very much correct. Thanks for catching that. Past my bedtime it would seem.
Oct 3 '06 #4

P: 23
No this is wrong sizeof can take a type or a variable and most people suggest using the variable name because then if the type changes the code stays correct with any further changes.

The problem lies in the way the structure is declared

Expand|Select|Wrap|Line Numbers
  1. struct {
  2.   int i;              // 4 bytes
  3.   char c;          // 1 byte
  4.   char b;          // 1 byte
  5. }a;
  6.  
  7. struct a p;
  8.  

This does not define a type of structure named a, it defines a variable a that is a structure you need

Expand|Select|Wrap|Line Numbers
  1. struct a {
  2.   int i;              // 4 bytes
  3.   char c;          // 1 byte
  4.   char b;          // 1 byte
  5. };
  6.  
  7. struct a p;
  8.  
This defines a type of structure named a which can then be used to define a variable p of that type.


P.S.

main()

should be

int main()

to avoid undefined behaviour
hai Banfa, really glad to see u explain the things from basics.But we have a rule that we dont need to give structure name,after the keyword struct,..in that case how i will have a structure definition and have a variable if that structure type
Oct 4 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
hai Banfa, really glad to see u explain the things from basics.But we have a rule that we dont need to give structure name,after the keyword struct,..in that case how i will have a structure definition and have a variable if that structure type
If you don't put a name after the struct keyword you have not named the structure type and will not be able to declare variables as

struct a p;

However if you don't want to name you structures you can use a typedef to define a new type for the structure

Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.   int i;              // 4 bytes
  3.   char c;          // 1 byte
  4.   char b;          // 1 byte
  5. } a;
  6.  
a is now a new type (although very poorly named) that is the structure, you can then declare variables

Expand|Select|Wrap|Line Numbers
  1. a p;
  2.  
Oct 4 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.