【问题标题】:Error in initializing members of a structure初始化结构成员时出错
【发布时间】:2015-02-19 03:35:36
【问题描述】:

下面是一个在c中初始化结构成员的程序

struct stack
{
    int *a;
    int top;
    int size;
}s;

void init()
{

    const int size =10; /*s.size=10;*/  /*const int s.size=10*/
    s.a=(int*)malloc(size*sizeof(int));
    s.top=0;    
}

int main()
{
    init();
    printf("Initialization done !\n");
    return 0;   
}

Q1 :当我写 s.size=10 时,在 init 方法而不是 const int size=10 中,我得到一个错误“size is not declared in scope”,但是我已经在stack struct 中声明了size。我能够以相同的方式初始化top 那么为什么会出错?

Q2:在init 方法中,我得到正确的输出 const int size=10。我很困惑,在这个语句中,我们如何能够在不使用结构变量的情况下访问结构stack 的成员size,不应该是const int s.size=10 吗?

【问题讨论】:

  • do not castmalloc()的返回值。
  • 编译器抱怨的不是s.size=10,而是你删除const int size=10s.a=(int*)malloc(size*sizeof(int));中的size

标签: c struct initialization malloc declaration


【解决方案1】:

是的,size 是一个结构变量,你必须使用结构变量访问并初始化它。

如果您初始化size =10,它将作为一个新变量。因为 init 函数将存储在单独的堆栈中,并且变量 size 的范围将仅在 init 函数内。 然后在分配内存时,您应该为 s.size 变量分配内存。

s.a = malloc(s.size * sizeof(int));

【讨论】:

    【解决方案2】:

    s.size=10 没问题。问题是当你为s.a分配内存时,没有名为size的变量,你应该把它改成:

    s.a = malloc(s.size * sizeof(int));
    

    您似乎对结构体struct stack s 中的变量size 和成员size 感到困惑,它们除了同名之外没有关系。

    【讨论】:

      【解决方案3】:

      我认为,您的困惑是因为您两次使用了相同的变量名size

      1. 作为结构成员变量
      2. 作为void init() 中的局部变量。

      请注意,这两个是独立的变量。

      struct stack 中的size 成员变量是结构的成员。您需要通过.-> 运算符访问成员变量[是的,即使结构是全局的]。

      OTOH,void init() 中的int sizeint 类型的普通变量。

      如果没有struct stack 类型的变量,就不存在属于struct stacksize。同样,您无法在任何地方直接访问size,它是struct stack 中的成员变量[不使用struct stack 类型的结构变量]。

      总结

      答案 1:

      该错误不适用于将const int size=10 替换为s.size = 10。而是从下一行开始,

      s.a= malloc(size*sizeof(int));
                    ^
                    |
      

      其中,当 const int size=10 被删除时,不存在 size 变量。

      答案 2

      const int size=10 声明并定义了一个名为size 的新变量。它与s.size [struct stack 的成员] 不同。这就是为什么使用

      s.a= malloc(size*sizeof(int));
                    ^
                    |
      

      有效,因为名为 size 的变量在范围内。

      【讨论】:

        【解决方案4】:
        Note: the following method of defining/declaring a struct is depreciated
        struct stack
        {
            int *a;
            int top;
            int size;
        }s;
        
        The preferred method is:
        // declare a struct type, named stack
        struct stack
        {
            int *a;
            int top;
            int size;
        };
        
        struct stack s;  // declare an instance of the 'struct stack' type
        
        // certain parameters to the compile command can force 
        // the requirement that all functions (other than main) 
        // have a prototype so:
        void init ( void );
        
        void init()
        {
        
            s.size =10;
            // get memory allocation for 10 integers
            if( NULL == (s.a=(int*)malloc(s.size*sizeof(int)) ) )
            { // then, malloc failed
                perror( "malloc failed" );
                exit( EXIT_FAILURE );
            }
        
            s.top=0;    
        } // end function: init
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-07
          相关资源
          最近更新 更多