简体   繁体   中英

What is the identifier of typedef struct

I came up to learn linked list. But, I'm quite confused with several ways of declaring linked lists with struct.

this is one way,

typedef struct Nodetag {
int dataNum;
struct Nodetag* nextNode;
} NODE;

and this is another way not using typedef

struct NODE {
int dataNum;
struct NODE* nextNode;
};

I'm curious, when we are declaring with typedef , I know the Nodetag is used to let the compiler know what struct Nodetag* nextNode; is. But then, what is the actual identifier of the struct ? Nodetag or NODE ? if it is Nodetag , when is NODE used?

You can optionally give a structure a tag, as in struct Nodetag or struct NODE . These tags (and union tags, and enum tags) are in a separate namespace from ordinary identifiers.

The typedef version creates an alias for struct Nodetag :

typedef struct Nodetag { ... } NODE;

Now NODE is a type name in the ordinary identifiers name space that is a synonym or alias for struct Nodetag .

Note that you can also write:

typedef struct Nodetag NODE;

struct Nodetag
{
    int   dataNum;
    NODE *nextNode;
};

The first line says 'there exists a structure type with the tag Nodetag and NODE is an alias for this type'. The second block says ' struct Nodetag consists of these items', listing a NODE * as one of the members.


C and C++ are two different languages

Note that this question is tagged C, and you are getting straight C answers (which is good). However, if you have encountered C++, you'd find that:

struct Nodetag
{
    int      dataNum;
    Nodetag *nextNode;
};

is valid C++ and generates a type name Nodetag in the ordinary identifiers name space (as well as the tag Nodetag in the (structure) tags namespace. This is not valid in C. You may get confused if you end up using a C++ compiler to compile C code, though.

This:

struct Nodetag {
    /* ... */
};

creates a type named struct Nodetag . Similarly, this:

struct NODE {
    /* ... */
};

creates a type named struct NODE .

In either case, you can wrap that declaration with a typedef declaration, creating a second name for the same type:

typedef struct S {
    /* ... */
} T;

This lets you refer to the type either as struct S or as T . (You can't call it just S -- though you could if you were programming in C++ rather than C.)

An equivalent way to write the above is:

struct S {
    /* ... */
};
typedef struct S T;

Note that struct tags and typedef names are in different namespaces (not in the C++ sense of the word "namespaces"), since a struct tag can only follow the keyword struct . So there's no need for them to be distinct.

typedef struct Node {
    /* ... */
} Node;

Now you can refer to the type either as struct Node or just as Node .

There's no great advantage in adding a typedef like this; if you like, you can omit it and just refer to the type as struct Node . (But a lot of C programmers like being able to use a one-word name for a type, and a typedef is the only good way to do that (a #define is another way to do it, but not a good way.)

It's also possible to omit the tag name, and just use the typedef:

typedef struct {
    /* ... */
} Node;

This gives you an anonymous struct type, and then immediately creates the name Node that refers to it. But with this approach, the struct can't contain a pointer to itself, since the name Node doesn't become visible until after the end of the struct definition.

The first struct is called struct Nodetag , the second struct is called struct NODE .

In the first instance, a typedef has been defined that "aliases" NODE for struct Nodetag , but it doesn't change the name of the structure. What it does do is let you type, for example, NODE* rather than struct Nodetag* . It's shorthand, nothing more.

Structure tags and types live in different namespaces. You can have a struct node and also a type node . Structure tags must be used with the struct specifier/prefix to distinguish them. When you do:

typedef struct Nodetag {
int dataNum;
struct Nodetag* nextNode;
} NODE;

You are defining a new type and also defining a structure tag but you don't really need to define a type it is just for convenience. Within the structure definition, since the compiler does not know about the type until it reads the } NODE; part, you have to use the structure tags to refer to the structure you are defining.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM