简体   繁体   English

使用malloc时的编译器错误(左值要求为赋值的左操作数)

[英]Compiler error (lvalue required as left operand of assignment) when using malloc

I got 2 structs,a pointer to each struct and a void **stack which points to the pointers of the structs. 我有2个结构,一个指向每个结构的指针,以及一个空**stack ,该**stack指向这些结构的指针。

my problem is at the line 我的问题是在线

(*ptr2+*a)=(struct student *)malloc(sizeof(struct student));

*a is a variable that increases by 1 everytime a stud registration happens so i don't allocate memory for the same address over and over again *a是一个变量,每次发生梭哈注册时都会增加1,所以我不会一遍又一遍地为同一地址分配内存

since i send the address of stud(&stud) at the menu function and then at the input function 因为我在菜单功能处然后在输入功能处发送stud(&stud)的地址

*ptr2==stud 

thus 从而

stud[*a]== *(stud+*a)== *(*ptr2+*a)

why is (*ptr2+*a) on the left side of malloc wrong? 为什么malloc左侧的(*ptr2+*a)错误? part of the code 部分代码

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);
            ...
}

It's wrong because you need (roughly) a variable on the left side of the assignment, not a value. 这是错误的,因为您需要(大致)在赋值左侧的变量,而不是值。 You can't write 1 = 2; 你不能写1 = 2; , but you can write int a; a = 1; ,但您可以编写int a; a = 1; int a; a = 1; . By the same logic, you can't write &a = (void*)0; 按照相同的逻辑,您不能写&a = (void*)0; .

Dereferencing a pointer gives you a variable, so you can write struct student *z = &a; *z = b; 取消引用指针可以为您提供一个变量,因此您可以编写struct student *z = &a; *z = b; struct student *z = &a; *z = b; .

If you want to write stud[*a] = malloc(...); 如果要编写stud[*a] = malloc(...); , but you don't have stud , only ptr2 , for which *ptr2 == stud holds, the correct way is, obviously, ,但您没有stud ,只有ptr2*ptr2 == stud适用,显然,正确的方法是

(*ptr2)[*a] = malloc(...);

And (*ptr2)[*a] == *(*ptr2 + *a) , so this would work as well: 并且(*ptr2)[*a] == *(*ptr2 + *a) ,所以这也可以工作:

*(*ptr2+*a) = malloc(sizeof(struct student));

my problem is at the line with 我的问题是

 (*ptr2+*a)=(struct student *)malloc(sizeof(struct student)); 

Do I cast the result of malloc? 我要转换malloc的结果吗?

I have edited this answer because I was completely wrong on the first try. 我已经编辑了此答案,因为第一次尝试时我完全错了。 In my head, there was one level of indirection more than you actually have. 在我的脑海中,间接性比您实际拥有的还要多。

Suppose you want to malloc() a number of entries and assign it to stud , ie write it to where stud actually sits. 假设您要malloc()多个条目并将其分配给stud ,即将其写入stud实际所在的位置。

If you were up to do it in main() , you'd do 如果您打算在main() ,则可以执行

struct student *stud = malloc(n * sizeof(*stud));

resp. RESP。

struct student *stud;
stud = malloc(n * sizeof(*stud));

if you want to have space for n entries. 如果要为n个条目留出空间。

If you want to do the same in the called function, then you replace stud with *ptr2 : 如果要在调用的函数中执行相同的操作,请使用*ptr2替换stud

*ptr2 = malloc(n * sizeof(*stud));

It seems you want to allocate only one entry here. 似乎您只想在此处分配一个条目。 Then you just do 那你就做

*ptr2 = malloc(sizeof(*stud));

Be aware that you have only one pointer which you can use to access either one entry (as you seem to want it), or an array of entries allocated appropriately. 请注意,只有一个指针可用于访问一个条目(如您所愿)或适当分配的一组条目。

While it is true that 的确

 stud[*a]== *(stud+*a)== *(*ptr2+*a) 

you are only allowed to access as many entries as you have allocated. 您只能访问分配的任意数量的条目。 Especially, if you have allocated only as much space as needed one entry, you are only allowed to use 特别是,如果您只分配了一个条目所需的空间,则只允许使用

stud[0] == *stud

both of which is a struct student and cannot be assigned the result of a malloc() call. 两者都是struct student ,因此无法分配malloc()调用的结果。

If you do an allocation of, eg, 如果您分配例如

malloc(10 * sizeof(*stud))

and assign that to stud or to *ptr , you can access more. 并将其分配给stud*ptr ,您可以访问更多。

OTOH, (*ptr2+*a) == (stud + *a) == &stud[*a] == &(*ptr2)[*a] . OTOH, (*ptr2+*a) == (stud + *a) == &stud[*a] == &(*ptr2)[*a] But this is nothing you can assign to, as the compiler tells you, this is not an lvalue. 但是,正如编译器告诉您的那样,这不是您可以分配的值,它不是左值。 And even if it was not an lvalue, you were not allowed to access it this way: while &stud[0] is exactly stud , &stud[1] points to the element after the one stud points to. 即使它不是左值,也不允许您以这种方式访问​​它: &stud[0]恰好是stud ,而&stud[1]指向一个stud指向的元素之后。

And as long as you haven't allocated enough for that to exist, this access is invalid for reading, and even so for writing: you cannot change the address of the 2nd element, as it always is the address of the first one plus sizeof(*stud) bytes. 而且,只要您没有分配足够的内存,该访问就对读取和写入都是无效的:您不能更改第二个元素的地址,因为它始终是第一个元素的地址加上sizeof(*stud)个字节。

I am really not completely clear about what you are up to; 我真的不太清楚你在做什么。 I suppose you want to allocate an array and to that in the wrong way. 我想您想以错误的方式分配数组并为其分配数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM