使用malloc时出现编译器错误(赋值的左操作数需要左值)
我得到了两个结构,一个指向每个结构的指针和一个指向结构指针的void使用malloc时出现编译器错误(赋值的左操作数需要左值),c,pointers,struct,malloc,pointer-to-pointer,C,Pointers,Struct,Malloc,Pointer To Pointer,我得到了两个结构,一个指向每个结构的指针和一个指向结构指针的void**堆栈 我的问题就在这条线上 (*ptr2+*a)=(struct student *)malloc(sizeof(struct student)); *a是一个变量,每当螺柱注册发生时,该变量会增加1,因此我不会一次又一次地为同一地址分配内存 因为我在菜单功能和输入功能中发送stud(&stud)的地址 *ptr2==stud 因此 为什么malloc左侧的(*ptr2+*a)错误? 部分守则 struct stude
**堆栈
我的问题就在这条线上
(*ptr2+*a)=(struct student *)malloc(sizeof(struct student));
*a
是一个变量,每当螺柱注册发生时,该变量会增加1,因此我不会一次又一次地为同一地址分配内存
因为我在菜单功能和输入功能中发送stud(&stud)
的地址
*ptr2==stud
因此
为什么malloc
左侧的(*ptr2+*a)
错误?
部分守则
struct student
{
char flag;
char surname[256];
int semester;
};
main()
{
...
struct student *stud;
menu(stack,stackcopy,reversedstack,&prof,&stud,stacksize,&head);
...
}
void menu(void **stack,void **stackcopy,void **reversed,struct professor **ptr1,struct student **ptr2,int size,int *head)
{
...
input(stack,ptr1,ptr2,size,head,&a,&b);
...
}
int input(void **stack,struct professor **ptr1,struct student **ptr2,int size,int *head,int *a,int *b)
{
...
done=Push(stack,head,(*(int *)ptr2+*a),size);
(*ptr2+*a)=(struct student *)malloc(sizeof(struct student));
stud_input(ptr2,a);
...
}
这是错误的,因为在赋值的左侧需要(大致上)一个变量,而不是一个值。你不能写1=2代码>,但您可以编写inta;a=1代码>。按照相同的逻辑,您不能编写&a=(void*)0代码>
取消对指针的引用会给您一个变量,因此您可以编写struct student*z=&a*z=b代码>
如果你想写stud[*a]=malloc(…)代码>,但您没有stud
,只有ptr2
,对于*ptr2==stud
,正确的方法显然是
(*ptr2)[*a] = malloc(...);
和(*ptr2)[*a]=*(*ptr2+*a)
,因此这也可以工作:
*(*ptr2+*a) = malloc(sizeof(struct student));
这是错误的,因为在赋值的左侧需要(大致上)一个变量,而不是一个值。你不能写1=2代码>,但您可以编写inta;a=1代码>。按照相同的逻辑,您不能编写&a=(void*)0代码>
取消对指针的引用会给您一个变量,因此您可以编写struct student*z=&a*z=b代码>
如果你想写stud[*a]=malloc(…)代码>,但您没有stud
,只有ptr2
,对于*ptr2==stud
,正确的方法显然是
(*ptr2)[*a] = malloc(...);
和(*ptr2)[*a]=*(*ptr2+*a)
,因此这也可以工作:
*(*ptr2+*a) = malloc(sizeof(struct student));
我的问题就在这条线上
(*ptr2+*a)=(struct student *)malloc(sizeof(struct student));
我编辑了这个答案,因为我在第一次尝试时完全错了。在我的脑海里,有一种比你实际拥有的更多的间接性
假设您想要malloc()
一系列条目,并将其分配给stud
,即。E把它写到stud
实际所在的位置
如果你能在main()
中做这件事,你会做的
struct student *stud = malloc(n * sizeof(*stud));
分别
如果要为n
条目留出空间
如果要在调用的函数中执行相同操作,则将螺柱
替换为*ptr2
:
*ptr2 = malloc(n * sizeof(*stud));
似乎您只想在此处分配一个条目。那你就这么做吧
*ptr2 = malloc(sizeof(*stud));
请注意,您只有一个指针,可以使用它访问一个条目(正如您所希望的那样)或适当分配的条目数组
虽然这是真的
您只能访问分配的条目数。特别是,如果您只分配了一个条目所需的空间,则只允许您使用
stud[0] == *stud
这两个函数都是struct student
,不能分配malloc()
调用的结果
如果你分配了,e。g、 ,
malloc(10 * sizeof(*stud))
并将其分配给螺柱
或*ptr
,您可以访问更多
OTOH,(*ptr2+*a)==(螺柱+*a)==&螺柱[*a]==&(*ptr2)[*a]
。但是,正如编译器告诉您的,这不是一个左值。即使它不是左值,也不允许以这种方式访问它:虽然&stud[0]
正是stud
,&stud[1]
指向stud
后面的元素
只要您没有分配足够的内存,这种访问对于读取是无效的,对于写入也是无效的:您不能更改第二个元素的地址,因为它总是第一个元素的地址加上sizeof(*stud)
字节
我真的不完全清楚你在干什么;我想您想要分配一个数组,并且以错误的方式分配给它
我的问题就在这条线上
(*ptr2+*a)=(struct student *)malloc(sizeof(struct student));
我编辑了这个答案,因为我在第一次尝试时完全错了。在我的脑海里,有一种比你实际拥有的更多的间接性
假设您想要malloc()
一系列条目,并将其分配给stud
,即。E把它写到stud
实际所在的位置
如果你能在main()
中做这件事,你会做的
struct student *stud = malloc(n * sizeof(*stud));
分别
如果要为n
条目留出空间
如果要在调用的函数中执行相同操作,则将螺柱
替换为*ptr2
:
*ptr2 = malloc(n * sizeof(*stud));
似乎您只想在此处分配一个条目。那你就这么做吧
*ptr2 = malloc(sizeof(*stud));
请注意,您只有一个指针,可以使用它访问一个条目(正如您所希望的那样)或适当分配的条目数组
虽然这是真的
您只能访问分配的条目数。特别是,如果您只分配了一个条目所需的空间,则只允许您使用
stud[0] == *stud
这两个函数都是struct student
,不能分配malloc()
调用的结果
如果你分配了,e。g、 ,
malloc(10 * sizeof(*stud))
并将其分配给螺柱
或*ptr
,您可以访问更多
OTOH,(*ptr2+*a)==(螺柱+*a)==&螺柱[*a]==&(*ptr2)[*a]
。但是,正如编译器告诉您的,这不是一个左值。即使它不是左值,也不允许以这种方式访问它:虽然&stud[0]
正是stud
,&stud[1]
指向stud
后面的元素
只要您还没有分配足够的资源使其存在,这种访问对于读取是无效的,对于写入也是无效的:您不能更改第二个元素的地址,因为它始终是第一个元素的地址