3

I have a code looking like this :

struct point {
   int a;
   int b;
}

which can be further used like this :

struct point p;
p.x = 10;
p.y = 5;

now I came to know that this can also be written like this :

typedef struct{
   int x;
   int y;
} point;

and can be used as point p

The confusion started when I started learning linked-list, this is the code I saw.

typedef struct node {
    int val;
    struct node * next;
} node_t;

I have a couple of questions:

  1. If we can define the struct by simply using typedef struct { ... } node whats the use of writing typedef struct node {.....
  2. The node_t at the end of the code is really confusing because from my understanding it has already defined a type node so we can call node x to create a node , then what's the need of the node_t or basically writing anything after the } what it means ?

shouldn't this work ?

typedef struct {
        int val;
        struct node * next;
    } node;
9
  • 4
    Don't you mean "struct p point" in your second code block? Commented Jun 24, 2014 at 12:42
  • 1
    You can't just do node x because node is a tag name. Commented Jun 24, 2014 at 12:44
  • @Happington Sir this is the code I found at learn-c.org/en/Structures, is there something wrong with it ? Commented Jun 24, 2014 at 12:44
  • 2
    first , You need to choose whether C++ or C. Commented Jun 24, 2014 at 12:46
  • 4
    In C++, you'd always use the first form (after adding the missing ;). The class name is a typename without the need for a typedef alias. But, despite the C++ tag, it looks like you're just asking about C. Commented Jun 24, 2014 at 12:51

4 Answers 4

7
  1. If we can define the struct by simply using typedef struct { ... } node whats the use of writing typedef struct node {.....

  2. The node_t at the end of the code is really confusing because from my understanding it has already defined a type node so we can call node x to create a node , then what's the need of the node_t or basically writing anything after the } what it means ?

In C, structures can have both tag and a typedef name. In the structure declaration:

typedef struct node {
    int val;
    struct node * next;
} node_t;   

node is a tag and node_t is typedef name. Latter you can declare your structure variable either by using tag or typedef name.

struct node *new_node;   // OK  
node_t *head;            // OK    

In fact, the tag and typedef name can even be same, although that's not required:

typedef struct node {
    int val;
    struct node * next;
} node;  

shouldn't this work ?

typedef struct {
    int val;
    struct node * next;
} node;  

No. This will not work . Why?
The reason is that, when a structure has a member that points to the same kind of structure, as node does, we are required to use a structure tag. Without the node tag, we would have no way to declare the type of next.


Suggested reading: As Op is asking for good resource on data structure. Here you can go:

  1. Tutorial: Introduction to Data Structures.
  2. Book: Classic Data Structures.
Sign up to request clarification or add additional context in comments.

11 Comments

thankyou for the great explanation. +1, can you also suggest me a good resource from where I can learn data structures
the last code example wont work, you are using node before its defined
@sp2danny; Oops! Corrected taht and adding wxplanation.
But you said that the typedef will handle that ?? @sp2danny, is the typedef not yet defined the node in this code ?
for a linked list, you need a tag, the typedef name dont exist yet inside the struct definition
|
4

The third block of code, which you seem to have a question about, has a redundant identifier.

typedef struct node {
    int val;
    struct node * next;
} node_t;

is identical to

struct node{
    int val;
    struct node * next;
};
typedef struct node node_t;

They're identical, but merged into the same line. The syntax around a typedef is

typedef [original name] [new name];

Normally, when we typedef a struct, the ONLY way to reference it afterwards would be the typedeffed name (in the first code example, you could only access it by node_t) while a slightly more redundant declaration allows for accessing by the "struct node" variable type too.

To answer your other question the node_t that confused you is what you can refer to the struct as... for example

node_t node1;
struct node node2;

Are the two valid declarations for a node struct.

5 Comments

so if I write typedef struct { int val; struct node * next; } node_t; will it be correct ?
No, you're calling it "struct node*" on the inside. At that point, there's no such thing as a "struct node." You can have it as a void pointer, and cast it as necessary.
Thanks a lot happington, for the first time in my extra small career of c I am feeling so clear
Then, as a small token of thanks. Please remove the c++ tag. c++ is NOT c
I just added the autotags given by SO. i ll remove it straight away .
0

Here, you can also use 'struct p' by typedefing it using 'struct p typedef P;'. It sometimes makes me confused when typedef a complex function pointer. I don't know the reason why the C std. support such a syntax sugar.

Comments

0

Here is my explanation.

struct
{
   int i;
};

declares an unnamed structure and apparently there is no real usage of this. Please correct me if I am wrong.

struct
{
   int i;
} T;

now T is an object in the given scope and type is unnamed.

struct TT
{
   int i;
} T;

declares a structure TT and an object T. You can use TT to create more objects. The best way to think about this is, just consider struct TT{ int i} as a single type such as double.

typedef struct TT
{
   int i;
};

has the same meaning as (at least in nowadays compiler. I'm not sure about the older ones)

struct TT
{
   int i;
};

and

typedef struct
{
   int i;
} TT;

declares a type TT (not an object) for the given structure as you are using typedef int Int;

Hope this resolves the mess.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.