structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • koder
    New Member
    • Sep 2006
    • 23

    structure

    i have different structure definitions.cou ld any one tell me why it is not taking the folowing variations

    Code:
    ex1:
    #include<stdio.h>
    
    struct{
    int i;              // 4 bytes
    char c;          // 1 byte
    char b;          // 1 byte
    };
    
    main()
     {
    struct a p;
    
    
    
      printf("%d",sizeof(p));
    }
    gcc produces the given error :storage size of `p' isn't known

    ex2.
    Code:
    #include<stdio.h>
    
    struct {
    int i;              // 4 bytes
    char c;          // 1 byte
    char b;          // 1 byte
    }a;
    
    main()
     {
    struct a p;
    
    
    
      printf("%d",sizeof(p));
    }
    gcc produces "storage size of `p' isn't known"

    could any one tell me why this above are wrong?
  • tyreld
    New Member
    • Sep 2006
    • 144

    #2
    You are using sizeof wrong. It expects a type to be passed to it, but you are passing an actual variable.

    Code:
    struct a p;
    
    // Wrong
    sizeof(p);
    
    // Correct
    sizeof(struct a);

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by tyreld
      You are using sizeof wrong. It expects a type to be passed to it, but you are passing an actual variable.

      Code:
      struct a p;
      
      // Wrong
      sizeof(p);
      
      // Correct
      sizeof(struct a);
      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

      Code:
      struct {
        int i;              // 4 bytes
        char c;          // 1 byte
        char b;          // 1 byte
      }a;
      
      struct a p;
      This does not define a type of structure named a, it defines a variable a that is a structure you need

      Code:
      struct [b]a[/b] {
        int i;              // 4 bytes
        char c;          // 1 byte
        char b;          // 1 byte
      };
      
      struct a p;
      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

      Comment

      • tyreld
        New Member
        • Sep 2006
        • 144

        #4
        Banfa, you are very much correct. Thanks for catching that. Past my bedtime it would seem.

        Comment

        • koder
          New Member
          • Sep 2006
          • 23

          #5
          Originally posted by Banfa
          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

          Code:
          struct {
            int i;              // 4 bytes
            char c;          // 1 byte
            char b;          // 1 byte
          }a;
          
          struct a p;

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

          Code:
          struct [b]a[/b] {
            int i;              // 4 bytes
            char c;          // 1 byte
            char b;          // 1 byte
          };
          
          struct a p;
          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

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by koder
            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

            Code:
            typedef struct {
              int i;              // 4 bytes
              char c;          // 1 byte
              char b;          // 1 byte
            } a;
            a is now a new type (although very poorly named) that is the structure, you can then declare variables

            Code:
            a p;

            Comment

            Working...