简体   繁体   English

尝试使指针为NULL时,赋值错误中的左值无效

[英]Invalid lvalue in assignment error when trying to make a pointer NULL

I have a pointer of a structure type that I made. 我有一个结构类型的指针,我做了。 On program start it begins as NULL and I then malloc/realloc as I need to add/remove these structures and I was just gonna use my pointer to point at the first structure and move through it like an array. 在程序启动它开始为NULL然后我malloc / realloc因为我需要添加/删除这些结构我只是用我的指针指向第一个结构并像数组一样穿过它。

When I malloc/realloc I always make the size of the "array"/area in memory one larger than it needs to be. 当我malloc / realloc时,我总是使内存中“数组”/区域的大小比它需要的大。 I do this so I can set the "last index"/area in memory to NULL so I can say something like while (pointer != NULL). 我这样做,所以我可以将内存中的“最后一个索引”/区域设置为NULL,这样我就可以说像while(指针!= NULL)。

I get the error: invalid lvalue in assignment when I try to assign NULL to the last position in the array/area of memory with the lines: 我得到错误:当我尝试将NULL分配给内存数组/内存区域中的最后一个位置时,赋值无效:

  // Realloc remotelist by adding one to connrhosts
  connrhosts++;
  remotelist = realloc(remotelist, sizeof(rhost)*(connrhosts + 1));
  (remotelist + connrhosts) = NULL;

What I think I am saying is: 我想我说的是:

  • Its time to add a new structure to my array so I will increase connrhosts by one. 是时候向我的阵列添加一个新结构了,所以我会将connrhosts增加一个。
  • Realloc the memory that is pointed at remotelist to a new area of memory that is the size of connrhosts (how many structures I will use) as well as one additional space so I can make it NULL 将指向remotelist的内存重新分配到一个新的内存区域,该区域是connrhosts的大小(我将使用多少个结构)以及一个额外的空间,所以我可以将其设为NULL
  • Point remotelist to the new area of memory 将remotelist指向新的记忆区域
  • Use my pointer remotelist and add the offset connrhosts which will now point to the last index of the area of memory and make that pointer NULL. 使用我的指针remotelist并添加偏移connrhosts,它现在将指向内存区域的最后一个索引并使该指针为NULL。

As far as I can tell (or feel) I did everything correctly, but I have been working on this project for sometime now and am under the impression I have tunnel vision. 据我所知(或感觉)我做的一切都是正确的,但我现在已经在这个项目上工作了一段时间,而且我的印象是我有隧道视野。 I would love to have a fresh set of eyes take a look at my logic/code and let me know what they think and what I did wrong. 我希望有一双新鲜的眼睛看看我的逻辑/代码,让我知道他们的想法和我做错了什么。 Thanks again. 再次感谢。 :D :d

Edit - Part of my problem is I think I have a misunderstanding of what I can do with pointers. 编辑 - 我的一部分问题是我认为我对使用指针做什么有误解。

Here is my structure: 这是我的结构:

typedef struct {
  char address[128]; // Buffer that holds our address of the remote host
  int port; // Port of the remote host
  int conn; // FD to the connection of our remote host
  int ofiles; // Open files associated with the remote host
} rhost;

What I was hoping I could do was loop through my array/area of memory and say if its not NULL then do something with it. 我希望我能做的是循环我的数组/内存区域,并说如果它不是NULL然后用它做一些事情。 So my original loop statement is while (NULL != remotelist). 所以我的原始循环语句是while(NULL!= remotelist)。 Now I believe are reading responses and comments that this logic is wrong because I am checking if a pointer is null? 现在我相信正在阅读这些逻辑错误的回复和评论,因为我正在检查指针是否为空? I ought to be checking if the area of memory/structure that the pointer is pointing is null? 我应该检查指针所指向的内存/结构区域是否为空? If this is the case it ought to be something like while (NULL != *(remotelist + someoffset))? 如果是这种情况,它应该像while(NULL!= *(remotelist + someoffset))?

I am doing it this way as my teacher suggested it/talked about it in class. 我正在这样做,因为我的老师在课堂上建议它/谈论它。

My initial declaration/initialization of remotelist was: rhost *remotelist = NULL; 我对remotelist的初始声明/初始化是:rhost * remotelist = NULL;

Errornous lvalue assignments occur when the LHS is an evaluated expression that does not become a variable that can be assigned. 当LHS是一个不能成为可分配变量的求值表达式时,会发生错误的左值赋值。 What you're doing looks like an operation (pointer arithmetic) which should be on the RHS. 你正在做什么看起来像应该在RHS上的操作(指针算术)。

What you can do is: 你能做的是:

remotelist[connrhosts] = NULL;  // array notation asuming 
                                // remotelist is an array of pointers

assuming connrhosts is a int or size_t or you could do: 假设connrhosts是intsize_t或者您可以这样做:

remotelist += connrhost; // pointer arithmetic
*remotelist = NULL; // assuming remotelist is an array of pointers.

You also need to dereference your pointer. 您还需要取消引用指针。

*(remotelist + connrhosts) = NULL;

Although I think 虽然我认为

 remotelist[connrhosts] = NULL; 

is clearer. 更清楚。

您需要使用*取消引用指针以访问存储在指针所寻址的内存中的内容。

*(remotelist + connrhosts) = NULL;

The expression pointer != NULL refers to the pointer itself, not the memory that the pointer references. 表达式pointer != NULL指的是指针本身,而不是指针引用的内存。 Even if you could assign NULL to it, that won't solve the problem. 即使您可以为其分配NULL,也无法解决问题。

After incrementing the pointer, it isn't NULL, it has an address in it, the address of your last, extra, struct slot. 递增指针后,它不是NULL,它有一个地址,最后一个额外的struct槽的地址。

You could, I suppose, set this area to 0's with: 我想,你可以将这个区域设置为0,用:

memset(remotelist + connrhosts, 0, sizeof(rhost));

Then you could do something like p->field == 0 if this field is never 0 on a real struct... 那么你可以做一些像p->field == 0事情,如果这个字段在真正的结构上永远不是0 ...

Personally, I would handle this issue, if I understand it correctly, by either allocating a second array of pointers to the structures, or by just keeping track of how many I have, or, most likely, by using a collection such as a linked list or tree, that is, something with more graceful expansion operations than realloc(). 就个人而言,我会处理这个问题,如果我理解正确的话,可以通过为结构分配第二个指针数组,或者只跟踪我有多少,或者最有可能通过使用链接等集合来处理列表或树,即具有比realloc().更优雅的扩展操作的东西realloc().

"Area of memory/structure" cannot be assigned NULL and cannot be compared to NULL . “内存/结构区域”不能指定为NULL ,也不能与NULL进行比较。 NULL is only used with pointers. NULL仅用于指针。

If you want to set all fields of a [newly allocated] structure to zeros, in C89/90 the common idiom is to do it with the help of = { 0 } initializer: 如果你想将[新分配的]结构的所有字段都设置为零,那么在C89 / 90中,常见的习惯用法是在= { 0 }初始值设定项的帮助下完成:

const rhost ZERO_HOST = { 0 };
...
connrhosts++; 
remotelist = realloc(remotelist, connrhosts * sizeof *remotelist); 
remotelist[connrhosts] = ZERO_HOST; 

Or you can simply use memset (which is a hack). 或者你可以简单地使用memset (这是一个黑客)。

As for checking whether the entry in your array is all-zeroes... There's no built-in operation for that, although memcmp function can help 至于检查数组中的条目是否为全零...虽然memcmp函数可以帮助,但没有内置操作

if (memcmp(&remotelist[i], &ZERO_HOST, sizeof ZERO_HOST) == 0)
  /* All zeroes */;

(this is also a bit of a hack, albeit less "hackish" that the memset one). (这也是一个黑客,虽然不那么“hackish” memset one)。

but it is not normally done that way. 但通常不这样做。 And there's really no point in doing it that way. 这样做真的没有意义。 Normally, you should simply select just one field in your structure (a "primary" one) that can tell you whether the structure is "used: or not and compare just that single field to 0 通常,您应该只选择结构中的一个字段(“主要”字段),它可以告诉您结构是否“已使用:或者不是,只将该字段与0进行比较

if (remotelist[i].address[0] == '\0')
  /* Entry is not used */;

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

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