What is the use of Struct Tag name in C programming?

て烟熏妆下的殇ゞ 提交于 2020-04-13 05:01:43

问题


I want to know the actual use of struct tag_name in C programming. Without using the tag_name also i was getting output as with use of tag_name. I want the exact reason behind this process.

For ex:

//With tag_name st1
struct st1 { int x; char c;}x={100,'a'},y={70,'e'};

//Without any tag_name
struct { int x; char c;}x={100,'a'},y={70,'e'};

printf("x.x= %d \t x.c= %c \n",x.x,x.c);   //Output: x.x=100    x.c=a
printf("y.x= %d \t y.c= %c \n",y.x,y.c);   //Output: y.x=70     y.c=e

回答1:


In the first case :

struct st1 { 
    int x; 
    char c;
} x = {100, 'a'}, y = {70, 'e'};

you declare a type with name struct st1, and you also create two objects, of this type, x and y. So you can create objects of this type whenever you want, like this :

struct st1 obj1;

However in the second case :

struct { 
    int x; 
    char c;
} x = {100, 'a'}, y = {70, 'e'};

you create a struct and two objects, x and y, but you cannot access this struct again. This means that you cannot create any new objects of this type.




回答2:


A struct declaration tells the compiler how a struct looks like and optionally gives that struct a name. If you want to use a struct as a "type" in your code, it requires a name:

struct coordinate {
  int x;
  int y;
};

// Can now be used as a return type:
struct coordinate getCoordinate ( ) { ... }

// Can now be used as an argument:
void drawAtPoint ( struct coordinate point ) { ... }

// Can be used to create new constants or variables:
const struct coordinate kOrigin = { 0, 0 };
struct coordinate lastViewPosition;

Alternatively you can create a nameless struct and explicitly define it to be a new type using a type definition:

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

// Can now be used as a return type:
coordinate getCoordinate ( ) { ... }

// Can now be used as an argument:
void drawAtPoint ( coordinate point ) { ... }

// Can be used to create new constants or variables:
const coordinate kOrigin = { 0, 0 };
coordinate lastViewPosition;

But if you don't do either one, you cannot use that struct as a type since in C, the type of a struct is defined by its name and not by its data layout.

The following is valid C code:

struct coordinate {
  int x;
  int y;
};

struct coordinate startCoord = { 20, 80 };
struct coordinate endCoord = startCoord;

Yet the following is not:

struct {
  int x;
  int y;
} startCoord = { 20,  80 };

struct {
  int x;
  int y;
} endCoord = startCoord;

Compiler will fail with an error. It seems as if startCoord and endCoord have the same type in both examples, but that's not true. Two structs don't denote the same type just because they have an equal memory layout or equal field names. For a C compiler, startCoord and endCoord have different types in the later example and thus you cannot assign them as shown above as this requires that both of them have the same type.

So the only situation where you can skip naming your structs is when you directly declare variables of that struct type and there is no requirement to ever refer to that struct again as a data type throughout your code.




回答3:


If you use the tag, you can create more variables of type struct st1 (or types derived from that (e.g., struct st1*)) later:

struct st1 z, *zp;

while after:

struct { int x; char c;}x={100,'a'},y={70,'e'};

you can never create variables of this type ever again.

While you can do:

struct { int x; char c;} z = {80, 'f'}; 
//same layout and member names

it will have a type that's different from that of x's and y's as far as aliasing and type checking is concerned.

The tag allows you to reuse the type.

(Typedefing an anomyous struct

typedef struct { int x; char c;} st1;
st1 x,y,z;

is another way to reuse the type.)



来源:https://stackoverflow.com/questions/44180120/what-is-the-use-of-struct-tag-name-in-c-programming

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!