C 初始化结构成员时出错
下面是一个在c中初始化结构成员的程序 问题1:当我编写s.size=10时,在init方法中,而不是在const int size=10中,我得到了一个错误size没有在作用域中声明,但我已经在stack struct中声明了size。我能够以相同的方式初始化top,那么为什么会出现错误 问题2:在init方法中,我得到了const int size=10的正确输出。我很困惑,在这句话中,我们如何在不使用结构变量的情况下访问结构堆栈的size成员,它不应该是const int s.size=10吗 是,因为大小是一个结构变量,所以必须使用结构变量访问并初始化它 如果初始化size=10,它将作为一个新变量。因为init函数将存储在单独的堆栈中,并且变量大小的范围将仅在init函数内部。 然后,在分配内存时,应该为s.size变量分配内存C 初始化结构成员时出错,c,struct,initialization,malloc,declaration,C,Struct,Initialization,Malloc,Declaration,下面是一个在c中初始化结构成员的程序 问题1:当我编写s.size=10时,在init方法中,而不是在const int size=10中,我得到了一个错误size没有在作用域中声明,但我已经在stack struct中声明了size。我能够以相同的方式初始化top,那么为什么会出现错误 问题2:在init方法中,我得到了const int size=10的正确输出。我很困惑,在这句话中,我们如何在不使用结构变量的情况下访问结构堆栈的size成员,它不应该是const int s.size=10
s.a = malloc(s.size * sizeof(int));
是的,因为大小是一个结构变量,所以必须使用结构变量访问并初始化它
如果初始化size=10,它将作为一个新变量。因为init函数将存储在单独的堆栈中,并且变量大小的范围将仅在init函数内部。
然后,在分配内存时,应该为s.size变量分配内存
s.a = malloc(s.size * sizeof(int));
s、 大小=10没有问题。问题是,当您为s.a分配内存时,没有名为size的变量,您应该将其更改为:
s.a = malloc(s.size * sizeof(int));
您似乎对结构结构堆栈s中的变量大小和成员大小感到困惑,它们除了具有相同的名称外没有任何关系。s.size=10没有问题。问题是,当您为s.a分配内存时,没有名为size的变量,您应该将其更改为:
s.a = malloc(s.size * sizeof(int));
您似乎对结构结构堆栈s中的变量大小和成员大小感到困惑,它们除了具有相同的名称外没有任何关系。我认为,您的困惑是因为您使用了相同的变量名称大小两次 作为结构成员变量 作为void init中的局部变量。 请注意,这两个变量是独立的 结构堆栈中的size成员变量是结构的成员。您需要通过以下任一方式访问成员变量。或->运算符[是,即使结构是全局的] OTOH,void init中的int size是int类型的正常变量 如果没有struct stack类型的变量,则不存在属于struct stack的大小。同样,您无法在任何地方直接访问size,它是结构堆栈中的一个成员变量[不使用struct stack类型的结构变量] 概括 答复1: 错误不在于将常量int size=10替换为s.size=10。这是从下一行开始的
s.a= malloc(size*sizeof(int));
^
|
其中,删除const int size=10时不存在大小变量
答复2
const int size=10声明并定义一个名为size的新变量。它与s.size[结构堆栈的成员]不同。这就是为什么使用
s.a= malloc(size*sizeof(int));
^
|
是有效的,因为名为size的变量在范围内。我认为,您的困惑是因为您已经使用了相同的变量名size两次
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
作为结构成员变量
作为void init中的局部变量。
请注意,这两个变量是独立的
结构堆栈中的size成员变量是结构的成员。您需要通过以下任一方式访问成员变量。或->运算符[是,即使结构是全局的]
OTOH,void init中的int size是int类型的正常变量
如果没有struct stack类型的变量,则不存在属于struct stack的大小。同样,您无法在任何地方直接访问size,它是结构堆栈中的一个成员变量[不使用struct stack类型的结构变量]
概括
答复1:
错误不在于将常量int size=10替换为s.size=10。这是从下一行开始的
s.a= malloc(size*sizeof(int));
^
|
其中,删除const int size=10时不存在大小变量
答复2
const int size=10声明并定义一个名为size的新变量。它与s.size[结构堆栈的成员]不同。这就是为什么使用
s.a= malloc(size*sizeof(int));
^
|
有效,因为名为size的变量在作用域中。请输入malloc的返回值。编译器抱怨的不是s.size=10,而是s.a=int*mallocsize*sizeofint;删除常量int size=10时,请返回malloc的值。编译器抱怨的不是s.size=10,而是s.a=int*mallocsize*sizeofint中的大小;删除常量int size=10时
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