21

What is the difference between these two declarations, if someone could explain in detail:

typedef struct atom {
  int element;
  struct atom *next;
};

and

typedef struct {
  int element;
  struct atom *next;
} atom;
user2780061
  • 313
  • 1
  • 2
  • 4
  • Check difference [one](http://codepad.org/BFWrrQET) and [two](http://codepad.org/aV0Dpf8z), in first [typedef is redundant](http://codepad.org/zfvEXthM) – Grijesh Chauhan Sep 14 '13 at 21:30
  • 3
    The first one is not valid. The whole point of `typedef` is to give a new name to a type declaration, and you haven't given any name to the structure type. – Barmar Sep 14 '13 at 21:33
  • @GrijeshChauhan Kind of raises more questions than it answers. – user2780061 Sep 14 '13 at 21:41
  • @Barmar I'm kind of a beginner in C so if you could explain it a bit more 'plain'. I understood typedef was meant to avoid having to type struct every time you created a new struct~. I was going after the fact struct with name before the braces specifies a type and name after braces specifies a "variable" with that name, I thought it applied to typedef struct too, obviously I've made a serious thinking error here somewhere. – user2780061 Sep 14 '13 at 21:41
  • Why @Barmar ? Why is it redundant? – user129393192 May 06 '23 at 18:04
  • Why what? I never said redundant, that was Grijesh Chauhan. @user129393192 – Barmar May 06 '23 at 18:07

3 Answers3

21

This is Normal structure declaration

  struct atom {
      int element;
      struct atom *next;
    };    //just declaration

creation of object

 struct atom object; 

  struct atom {
      int element;
      struct atom *next;
    }object;    //creation of object along with structure declaration

And

This is Type definition of struct atom type

typedef  struct atom {
  int element;
  struct atom *next;
}atom_t;  //creating new type

Here atom_t is alias for struct atom

creation of object

atom_t object;      
struct atom object; //both the ways are allowed and same
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
  • `typedef` is a keyword . The purpose of typedef is to form complex types from more-basic machine types.SEE THIS INFO typedef http://en.wikipedia.org/wiki/Typedef – Gangadhar Sep 14 '13 at 21:54
16

The purpose of typedef is to give a name to a type specification. The syntax is:

typedef <specification> <name>;

After you've done that, you can use <name> much like any of the built-in types of the language to declare variables.

In your first example, you the <specification> is everything starting with struct atom, but there's no <name> after it. So you haven't given a new name to the type specification.

Using a name in a struct declaration is not the same as defining a new type. If you want to use that name, you always have to precede it with the struct keyword. So if you declare:

struct atom {
    ...
};

You can declare new variables with:

struct atom my_atom;

but you can't declare simply

atom my_atom;

For the latter, you have to use typedef.

Note that this is one of the notable differences between C and C++. In C++, declaring a struct or class type does allow you to use it in variable declarations, you don't need a typedef. typedef is still useful in C++ for other complex type constructs, such as function pointers.

You should probably look over some of the questions in the Related sidebar, they explain some other nuances of this subject.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Oh, that explains it, it more related to typedef than to struct. One other question; whats the deal when there is a a word at both places: `code` typedef struct at { int element; struct at *next; } atom; – user2780061 Sep 14 '13 at 22:26
  • 1
    That allows you to use either `struct at` or `atom` to declare variables. – Barmar Sep 14 '13 at 22:28
  • Meaning this is the sole purpose of this? – user2780061 Sep 14 '13 at 22:29
  • 1
    You need to give the `struct` a name so that you can reference it in the `next` link, because the `typedef` isn't defined until after the statement. – Barmar Sep 14 '13 at 22:33
  • See this question regarding typedef and linked lists: http://stackoverflow.com/questions/3988041/struct-containing-pointers-to-itself – Barmar Sep 14 '13 at 22:33
1

The general syntax of typedef keyword will be: typedef existing_data_type new_data_type;

typedef struct Record {
    char ename[30];
     int ssn;
    int deptno;
} employee;
Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
Deep
  • 11
  • 2