6

我在尝试嵌套需要声明为新 var 类型的结构时遇到问题。代码如下 -

typedef struct
{
    typedef struct
    {
        int day,
            month,
            year;
    } Date;

    Date manuDate,
         purDate;
    double purPrice;
} Car;

除非我尝试编译它会向我抛出一个错误说

“typedef 之前的语法错误”以及由于进一步下降而导致的一系列其他错误。

这是C不能做的吗?我知道嵌套结构在没有指针的情况下存在问题,但我不确定在这种情况下如何工作......

4

5 回答 5

7

C 不支持嵌套结构定义。也许您正在查看一些 C++ 代码。

相反,您只需先定义“内部”结构,然后在“外部”结构中引用它。

typedef struct
{
    int day,
        month,
        year;
} Date;


typedef struct
{
    Date manuDate,
         purDate;
    double purPrice;
} Car;
于 2015-05-04T00:12:12.823 回答
5

C 确实有嵌套的结构/联合/枚举,但没有嵌套的 typedef。

struct A { typedef struct B { int x; } B; }; //WRONG
struct A { struct { int x; } b; }; //OK - an anonymous nested struct w/ an instance
struct A { struct { int x; }; }; //OK - an anonymous nested struct w/out an instance; x effectively belongs to `struct A`
struct A { struct B { int x; } b; }; //OK, the `struct B` type also becomes available in the outer scope
struct A { struct B { int x; }; }; //WRONG, an tagged nested struct needs at least 1 instance

嵌套的结构/联合/枚举可以是匿名的(未标记),在这种情况下它们成为外部结构/联合的一部分或被标记。

匿名内部结构/联合也可以在不定义实例的情况下逃脱,在这种情况下,内部成员递归地成为外部结构/联合的成员。

标记的嵌套结构/联合/枚举需要实例,它就像带有实例的匿名嵌套结构/联合/枚举,除了标记类型也可供以后使用,表现得好像它是一个独立的外部范围结构/联合/枚举定义.

将标记的 struct/union/enum 简单地放在外部范围中而不是将其混淆地嵌套在另一个 struct/union 中可能是明智的。

于 2020-08-29T23:51:24.890 回答
-1

I had the same issue; I just used pointers to access my typedef structs; I used Glib/Gtk+ Development Platform part II - Object-Oriented programming in C

//*lul.h
typedef struct _lultype {
    char **lularray; 
} lultype;

typedef struct _lul { 
    lultype *mylul;   //pointer to lultype struct
 } lulmon_t;

 //*lul.c
 lulmon *initialize_lul () {
    lulmon_t *lulmon = malloc(sizeof(lulmon_t));
    lulmon->mylul = malloc(sizeof(lultype)); 
    lulmon->mylul->lularray = calloc(8, sizeof(char)); 
    return lulmon; // returns the address of lulmon. 
 }

 // main.c

 int main() {
    lulmon *lulmon = initialize_lul(); 
    /* store characters inside my lularray */
    *(lulmon->mylul->lularray + 0) = 0x01;
    *(lulmon->mylul->lularray + 1) = 0x02;
     lulmon->mylul->lularray[2] = 0x03; //alternative way of accessing index
    return 0; 
 }
于 2020-12-21T17:31:48.977 回答
-1

C 确实支持嵌套结构,但您不能在外部使用嵌套结构作为参考。

typedef struct
{
    typedef struct Date
    {
        int day,
            month,
            year;
    } manuDate, purDate;

    double purPrice;
} Car;


Car car;

car.manuDate.year = 2020;
car.manuDate.month = 1;
car.manuDate.day = 1;
car.purDate.year = 2020;
car.purDate.month = 2;
car.purDate.day = 14;


car.purPrice = 15000.00;

我刚刚在 1996 年的 C 编译器(VisualAge C)上尝试过,它运行良好。

于 2020-08-29T23:21:52.120 回答
-1

它不起作用,因为这严格来说是一个 OOP 功能。C 不是 OO。如果从内部结构中删除 'typedef' 并将其在 'inner' 声明中的使用替换为 'void*',它将编译。因此,如果您正在寻找疯狂的 OOP 样式嵌套,您将不得不利用您的 'void*' - 在 C 中没有其他方法可以做到这一点。

如果你真的想做嵌套的东西,你将不得不删除 typedef。您可以做的是使用未命名的结构在主结构中创建结构化变量。

例子:

typedef struct ObjectType {
    float width;
    float height;
    float x;
    float y;

    //Nested Struct
    struct {
        float r;
        float g;
        float b;
        float a;
    } color; //color is a variable

} Object;
于 2019-07-21T06:15:20.963 回答