【问题标题】:Is something like "typedef int arrChoice[5];" a global variable?类似于“typedef int arrChoice[5];”全局变量?
【发布时间】:2012-08-07 03:47:29
【问题描述】:
#include <stdio.h>
#include <conio.h>
typedef arrChoice[10] /*is this a global variable?*/
int main() {};

  getch();
  return 0;
}

它还没有完成,但这就是我的意思。

【问题讨论】:

  • typedef 不是一个变量,它只是按照它所说的做......定义一个类型。

标签: c arrays variables global


【解决方案1】:

typedef 不是全局变量,它只是另一种类型的别名。当我传递它们时,我通常将它们用作函数指针,因为每次都将它们写出来很烦人。

typedef int (*function)(int, int);

我还使用它们将结构、联合或枚举定义为类型

typedef struct {
    int x;
    int y;
    int z;
} Point;

【讨论】:

    【解决方案2】:

    typedef 声明新类型不是变量。

    【讨论】:

      【解决方案3】:

      这可能会对您有所帮助。在您在此处发布的代码中,有一个错误。侧主函数中没有语句。 getch 和 return 语句应该在 main 函数中。我觉得你的代码应该是这样的。

      #include <stdio.h>
      typedef int  arrChoice; /* arrChoice is alias to int */
      arrChoice a[10] ;/* array a is a global variable of integers*/
      int main()
      {
       getch();
       return 0;
      }
      

      请注意,typedef 的目的是为现有类型(int、float、double 等)分配替代名称。以下语句类似。

      typedef arrChoice[10]  is similar to typedef int[10];
      

      当您尝试引用 arrChoice 时,您会收到一条错误消息

      expected expression before 'arrChoice'.
      

      【讨论】:

        猜你喜欢
        • 2014-09-24
        • 2016-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多