Find the answer to your Linux question:
Results 1 to 4 of 4
i've the following structure definitions: typedef struct { char infoType; char infoLen; char data[128]; }InfoType1; struct InfoType2 { char infoType; char infoLen; char data[128]; }; As far as I know, ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    11

    difference between "typedef struct" and "struct"

    i've the following structure definitions:

    typedef struct
    {
    char infoType;
    char infoLen;
    char data[128];
    }InfoType1;

    struct InfoType2
    {
    char infoType;
    char infoLen;
    char data[128];
    };

    As far as I know, differences between these two are:

    --- when declaring of those types, we use
    InfoType1 i1;
    InfoType2 i2;

    --- we can specify variables of type InfoType2 at the definition of struct itself like:
    struct InfoType2
    {
    char infoType;
    char infoLen;
    char data[128];

    }t1,t2;
    But we can't specify variables of type InfoType1 at the definition of struct itself, a separate declaration is needed, like:
    InfoType1 i1;


    Are there any other differences between these two?
    y do we've "typedef struct" when there is structure facility for a collection of variables of different data types?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm not sure what that final line in your post means.

    But you have a misconception. The following will not compile:

    Code:
    int main(void)
    {
      typedef struct
      {
        char infoType;
        char infoLen;
        char data[128];
      } InfoType1;
    
      struct InfoType2
      {
        char infoType;
        char infoLen;
        char data[128];
      };
    
      InfoType1 i1;
      InfoType2 i2;
    
      return 0;
    
    } /* main() */
    Here's the error:

    Code:
    f.c: In function `main':
    f.c:18: `InfoType2' undeclared (first use in this function)
    f.c:18: (Each undeclared identifier is reported only once
    f.c:18: for each function it appears in.)
    f.c:18: syntax error before "i2"
    InfoType2 is not a data type; it's something I would call a "struct type". The following code will compile without error:

    Code:
    int main(void)
    {
      typedef struct
      {
        char infoType;
        char infoLen;
        char data[128];
      } InfoType1;
    
      struct InfoType2
      {
        char infoType;
        char infoLen;
        char data[128];
      };
    
      InfoType1 i1;
      struct InfoType2 i2;
    
      return 0;
    
    } /* main() */
    vijiambav, you want more information than can be easily presented in the context of a forum like this one. Please consider googling this:

    Code:
    C tutorial
    Hope this helps.

  3. #3
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    typedef isn't just for structs; I often find myself doing a

    Code:
    typedef char *String
    So I can declare stuff of type String. typedef effectively creates a new data type.

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Okay. The reason for this regards the way structs are handled.

    For some reason, when you declare the following:
    Code:
    struct s1 { ... };
    You must do this to recognize the type:
    Code:
    struct s1 myStruct;
    void myFunc(struct s1 variable) { ... }
    And so on. Note that you need to specify "struct s1". What we want to do is just say "s1". What typedef does is create a new data type that is exactly the same as another data type. For instance:
    Code:
    typedef int number
    We can now use "number" instead of "int" anywhere in the program. Similarly, we might do:
    Code:
    typedef struct s1 s1
    Now "s1" is equivalent to "struct s1" and can be used anywhere.

    But that's not all! Because we can declare anonymous structs, we can do the above in a single line:
    Code:
    typedef struct s1 { ... } s1
    This parses as follows:
    Code:
    [ typedef [ struct s1 { ... } ] s1 ]
    Note how "struct s1 { ... }" is considered the type that we are aliasing to, and s1 is the new alias.

    Does this make sense?
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...