【问题标题】:Nested typedef structs嵌套 typedef 结构
【发布时间】:2015-07-13 06:59:49
【问题描述】:

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

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

    Date manuDate,
         purDate;
    double purPrice;
} Car;

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

“typedef 之前的语法错误”以及由于该错误导致的一系列其他错误。

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

【问题讨论】:

    标签: c ansi-c


    【解决方案1】:

    我有同样的问题;我只是使用指针来访问我的 typedef 结构; 我使用了 Glib/Gtk+ 开发平台 第二部分 - 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; 
     }
    

    【讨论】:

    • 欢迎来到 SO。您的回答应该解释错误的原因以及您已修复的内容。仅显示一些示例信息量不大。此外,您的代码包含未定义的行为:lulmon->mylul->lularray = calloc(8, 8*(sizeof(char)); 在取消引用之前,您尚未为 lulmon->mylul 分配任何值。此外,您对void** 的分配非常奇怪。它应该以某种方式包含指针的大小,另一方面它可能不应该在元素数量的参数中包含char 的大小。最后:为什么要返回 void* 而不是 lulmon_t*
    • ty 指出他们,我在 10 秒内写了这个;我修复了一些 uwu
    • 这仍然没有多大意义:calloc(8, 8*(sizeof(char));。你有一个char**。这应该是这样的:calloc(sizeof (char*), number_of_pointers);
    • 也在这里:*(lulmon->mylul->lularray + 0) = 0x01;lularray+0 指向哪里?您还没有为lularray[0] 等分配内存。总的来说,我认为这个答案比帮助更令人困惑。特别是它没有添加几年前给出的答案中尚未解决的任何内容。
    【解决方案2】:

    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
    

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

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

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

    将标记的结构/联合/枚举简单地放在外部范围中而不是将其混淆地嵌套在另一个结构/联合中可能是明智的。

    【讨论】:

      【解决方案3】:

      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) 上尝试过,效果很好。

      【讨论】:

        【解决方案4】:

        它不起作用,因为那是严格的 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;
        

        【讨论】:

          【解决方案5】:

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

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

          typedef struct
          {
              int day,
                  month,
                  year;
          } Date;
          
          
          typedef struct
          {
              Date manuDate,
                   purDate;
              double purPrice;
          } Car;
          
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多