Sometimes I see code like this (I hope I remember it correctly):
有时我看到这样的代码(我希望我能正确地记住它):
typedef struct st {
int a; char b;
} *stp;
While the usual pattern that I familiar with, is:
而我所熟悉的通常模式是:
typedef struct st {
int a; char b;
} st;
So what's the advantage in the first code example?
那么第一个代码示例的优点是什么呢?
5 个解决方案
#1
6
You probably mean this:
你的意思是这样的:
typedef struct ST {
/* fields omitted */
} *STP;
The asterisk is at the end of the statement. This simply means "define the type STP to be a pointer to a struct of this type". The struct tag (ST
) is not needed, it's only useful if you want to be able to refer to the struct type by itself, later on.
星号在语句的末尾。这仅仅意味着“将STP类型定义为指向此类结构体的指针”。不需要struct标记(ST),只有当您希望稍后能够自己引用struct类型时,它才有用。
You could also have both, like so:
你也可以同时拥有两者,比如:
typedef struct {
/* fields omitted */
} ST, *STP;
This would make it possible to use ST
to refer to the struct type itself, and STP
for pointers to ST
.
这将使使用ST引用struct类型本身成为可能,而STP引用指向ST的指针成为可能。
Personally I find it a very bad practice to include the asterisk in typedefs, since it tries to encode something (the fact that the type is a pointer) into the name of the type, when C already provides its own mechanism (the asterisk) to show this. It makes it very confusing and breaks the symmetry of the asterisk, which appears both in declaration and use of pointers.
我个人认为在typedefs中包含星号是一种非常糟糕的做法,因为它试图将某些东西(类型是指针)编码到类型的名称中,而C已经提供了自己的机制(星号)来显示这一点。它使它非常混乱,破坏了星号的对称性,星号在声明和指针的使用中都出现。
#2
3
It's a habit that stems from the time when typedef names and struct tagnames were in the same namespace. See http://blogs.msdn.com/oldnewthing/archive/2008/03/26/8336829.aspx
这是一种习惯,源于定义类型名称和结构标记名称位于同一名称空间的时代。参见http://blogs.msdn.com/oldnewthing/archive/2008/03/26/8336829.aspx
#3
2
I think you are talking about :
我想你说的是
typedef struct{
int a;
char b;
} object, *objectPointer;
This means that (new) type objectPointer is a pointer to struct
(object) defined above. Its easy to declare pointers to object struct
this way. For instance,
这意味着(new)类型objectPointer是上面定义的struct(对象)的指针。以这种方式声明指向对象结构体的指针很容易。例如,
objectPointer A = (objectPointer)malloc(sizeof(object));
A->a = 2;
Now, A is a pointer to struct object and you can access its variables as described above.
In case, objectPointer was not defined,
现在,A是指向struct对象的指针,您可以访问它的变量,如上面所述。如果没有定义objectPointer,
struct object *A = (struct object *)malloc(sizeof(object));
A->a = 2;
So, I guess objectPointer is more
intuitive and
easy to use.
#4
1
I hope that the first code would say a compiler error ,
我希望第一个代码会说编译错误,
#5
0
I see no good reason for the typedef name be different from the tag name.
我看不出定义名称与标记名称有什么不同的好理由。
Now, the reason for which the tag name needs to be typedefed if you don't want to use
现在,如果您不想使用标记名,需要对其进行类型化的原因
struct tag v;
but
但
tag v;
is probably an historical one. For as long as I remember, C had typedef but I don't know if it was true when struct
have been introduced (handling of typedef is a nuisance in the C grammar). In the old code I've seen, using typedef for struct isn't done, and there are things like unix
可能是一个历史问题。就我所知,C有typedef,但我不知道在引入struct时它是否正确(处理typedef在C语法中很麻烦)。在我看到的旧代码中,还没有为struct使用typedef,还有unix之类的东西
struct stat;
int stat(const char*, struct stat*);
which would break with an automatic typedef. One those are introduced, changing is quite difficult (yes, C++ has automatic typedef but C++ has special wording to handle that case of overloading and it would be yet another complication).
这将打破自动定义。其中一个是引入的,更改是相当困难的(是的,c++有自动的typedef,但是c++有特殊的措辞来处理重载的情况,这将是另一个复杂的问题)。
#1
6
You probably mean this:
你的意思是这样的:
typedef struct ST {
/* fields omitted */
} *STP;
The asterisk is at the end of the statement. This simply means "define the type STP to be a pointer to a struct of this type". The struct tag (ST
) is not needed, it's only useful if you want to be able to refer to the struct type by itself, later on.
星号在语句的末尾。这仅仅意味着“将STP类型定义为指向此类结构体的指针”。不需要struct标记(ST),只有当您希望稍后能够自己引用struct类型时,它才有用。
You could also have both, like so:
你也可以同时拥有两者,比如:
typedef struct {
/* fields omitted */
} ST, *STP;
This would make it possible to use ST
to refer to the struct type itself, and STP
for pointers to ST
.
这将使使用ST引用struct类型本身成为可能,而STP引用指向ST的指针成为可能。
Personally I find it a very bad practice to include the asterisk in typedefs, since it tries to encode something (the fact that the type is a pointer) into the name of the type, when C already provides its own mechanism (the asterisk) to show this. It makes it very confusing and breaks the symmetry of the asterisk, which appears both in declaration and use of pointers.
我个人认为在typedefs中包含星号是一种非常糟糕的做法,因为它试图将某些东西(类型是指针)编码到类型的名称中,而C已经提供了自己的机制(星号)来显示这一点。它使它非常混乱,破坏了星号的对称性,星号在声明和指针的使用中都出现。
#2
3
It's a habit that stems from the time when typedef names and struct tagnames were in the same namespace. See http://blogs.msdn.com/oldnewthing/archive/2008/03/26/8336829.aspx
这是一种习惯,源于定义类型名称和结构标记名称位于同一名称空间的时代。参见http://blogs.msdn.com/oldnewthing/archive/2008/03/26/8336829.aspx
#3
2
I think you are talking about :
我想你说的是
typedef struct{
int a;
char b;
} object, *objectPointer;
This means that (new) type objectPointer is a pointer to struct
(object) defined above. Its easy to declare pointers to object struct
this way. For instance,
这意味着(new)类型objectPointer是上面定义的struct(对象)的指针。以这种方式声明指向对象结构体的指针很容易。例如,
objectPointer A = (objectPointer)malloc(sizeof(object));
A->a = 2;
Now, A is a pointer to struct object and you can access its variables as described above.
In case, objectPointer was not defined,
现在,A是指向struct对象的指针,您可以访问它的变量,如上面所述。如果没有定义objectPointer,
struct object *A = (struct object *)malloc(sizeof(object));
A->a = 2;
So, I guess objectPointer is more
intuitive and
easy to use.
#4
1
I hope that the first code would say a compiler error ,
我希望第一个代码会说编译错误,
#5
0
I see no good reason for the typedef name be different from the tag name.
我看不出定义名称与标记名称有什么不同的好理由。
Now, the reason for which the tag name needs to be typedefed if you don't want to use
现在,如果您不想使用标记名,需要对其进行类型化的原因
struct tag v;
but
但
tag v;
is probably an historical one. For as long as I remember, C had typedef but I don't know if it was true when struct
have been introduced (handling of typedef is a nuisance in the C grammar). In the old code I've seen, using typedef for struct isn't done, and there are things like unix
可能是一个历史问题。就我所知,C有typedef,但我不知道在引入struct时它是否正确(处理typedef在C语法中很麻烦)。在我看到的旧代码中,还没有为struct使用typedef,还有unix之类的东西
struct stat;
int stat(const char*, struct stat*);
which would break with an automatic typedef. One those are introduced, changing is quite difficult (yes, C++ has automatic typedef but C++ has special wording to handle that case of overloading and it would be yet another complication).
这将打破自动定义。其中一个是引入的,更改是相当困难的(是的,c++有自动的typedef,但是c++有特殊的措辞来处理重载的情况,这将是另一个复杂的问题)。