c - 不同类型 ('struct TGAME' 与 'struct TGAME' 的 Typedef 重定义)

标签 c

我遇到了标题的编译错误。

我听说这可能是因为我给结构的名称,但我将它更改为随机名称太多次,但并没有解决它。

我的 IDE (CLion) 在 PLBR.c 中显示错误

这是我的文件:

PLBR.h

#ifndef UNTITLED2_PLBR_H
#define UNTITLED2_PLBR_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    int players;
    char diff[5];
    int numpal;
    char *palabras;
}TGAME;

PLBR.c

#include "PLBR.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
    int players;
    char diff[5];
    int numpal;
    char *palabras;
}TGAME;

在 main.c 中,我只有 #include "PLBR.h"和一些代码,但没有任何关系。

最佳答案

简单的部分

这是一个有趣的问题,因为完整地回答它很复杂。让我们从简单的答案开始:为避免出现问题,请勿在源文件中重新定义类型。在头文件中定义它并在源文件中包含头文件就足够了。

复杂的部分

现在是复杂的部分。您可以重复 typedef定义,如果它将名称重新定义为同一类型。 C 2018 6.7 3 说:

… there shall be no more than one declaration of the identifier… except that:

— a typedef name may be redefined to denote the same type as it currently does, provided that type is not a variably modified type;…

所以,如果你有 typedef int foo;两次或 typedef char bar[3];两次,没关系。你也可以有 typedef struct MyStruct foo;两次。

但是,您不能拥有 typedef struct { int i; } foo;两次。尽管这两个声明具有相同的文本,但它们声明了不同的类型。这是因为 C 2018 6.7.2.3 5 中的一条规则:

… Each declaration of a structure, union, or enumerated type which does not include a tag declares a distinct type.

原因是有时我们出于不同目的使用具有相同内容的结构。例如,我们可能有一个包含两个 double 的结构。我们用于复数(实部和虚部)的值和具有两个 double 的结构我们用于平面中点的值(xy 坐标):

typedef struct { double d[2]; } ComplexNumber;
typedef struct { double d[2]; } Point;

让编译器将它们视为不同的类型意味着它可以向我们发出有关错误的警告,例如传递 Point。作为 Complex 的参数预计。

因此每次重复定义时,您的结构都是不同的类型。

有一种方法可以多次引用同一个结构类型,那就是给它一个标签:

typedef struct MyTag {
    int players;
    char diff[5];
    int numpal;
    char *palabras;
} TGAME;

然后我们可以重新定义TGAME没有错误; typedef struct MyTag TGAME;可能出现,甚至多次,它重新定义了TGAME是同一类型,struct MyTag , 这是允许的。

但是,尽管您可以重复 typedef ,您不能重复结构定义。如果你有两次:

typedef struct MyTag {
    int players;
    char diff[5];
    int numpal;
    char *palabras;
} TGAME;

编译器会报错。这是因为 C 2018 6.7.2.3 说:

A specific type shall have its content defined at most once.

因此,尽管您可以重复 typedef使用相同的类型多次,您不能重复完整的结构定义。要再次使用结构类型,您只能单独使用标记重复它,而不能使用其内容的完整定义。

更多并发症

在所有这些之后,关于结构标签和类型的规则仍然不完整。仍有范围问题需要考虑。使用 struct MyTag在新范围内可以引用以前的类型或创建新类型,具体取决于它的使用方式!

这是一个例子。以下得到一个错误,因为 typedef TypeA TypeB;不重新定义 TypeB作为同一类型,它首先被定义为:

typedef struct Tag { int x; } MyType;   // Create a new type.

void foo(void)  //  Start a new scope.
{
    typedef struct Tag TypeA;               // Define TypeA as previous type.
    typedef struct Tag { int x; } TypeB;    // Make a new type.
    typedef TypeA TypeB;                    // Error, TypeA is not same as TypeB.
}

但插入 struct Tag;改变意义:

typedef struct Tag { int x; } MyType;   // Create a new type.

void foo(void)  //  Start a new scope.
{
    struct Tag;                             // Declare struct Tag to be some new type.
    typedef struct Tag TypeA;               // Define TypeA as new type.
    typedef struct Tag { int x; } TypeB;    // Define new type and define TypeB to be that type.
    typedef TypeA TypeB;                    // Works, TypeA is same as TypeB.

    (void) (TypeB *) 0; // (Avoid compiler warning about unused name.)
}

这是因为 struct <i>identifier</i>; 有一条特殊规则就那样出现,没有{ … }定义成员或作为更大声明的一部分,如 typedef .该规则是 C 2018 6.7.2.3 7,它说:

A declaration of the form

<i>struct-or-union</i> <i>identifier</i> ;

specifies a structure or union type and declares the identifier as a tag of that type.

所以 struct Tag;说“我们正在宣布一个新的 struct Tag在此范围内输入。”相反,当我们有 typedef struct Tag TypeA;没有struct Tag; , 那么 C 2018 6.7.2.3 9 适用。它说如果 struct <i>identifier</i> “发生的不是上述形式之一,并且标识符作为标记的声明是可见的,然后它指定与其他声明相同的类型,并且不会重新声明标记。”它所指的“以上形式”是struct <i>identifier</i>;我上面有struct <i>identifier</i><sub>opt</sub> { <i>struct-declaration-list</i> } ,它通过定义其成员来定义新类型。所以struct Tag出现在 typedef struct Tag TypeA;除了这些形式之一,以及之前的struct Tag是可见的,所以使用 struct Tag指定与前一个相同的类型,而不是新类型。

关于c - 不同类型 ('struct TGAME' 与 'struct TGAME' 的 Typedef 重定义),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66760207/

相关文章:

C:指向结构定义中结构的指针

c - 我在与 c 中的整数值进行比较时遇到问题。无法使用 '==' 运算符进行比较

c - 传递 'functions' 的参数 1 从一个没有转换的整数中生成一个指针

c - 在 Linux C 控制台应用程序中模拟按键

c - 参数是如何加载到 $esp 中的?

c - 在 Delphi 中加入 16 位整数以生成 32 位整数?

c - 为什么我必须额外调用 `getchar()`?

c - 请求成员不是结构或 union 错误

c - 在 C 中释放链表

c - 条件表达式中的指针/整数类型不匹配