Invalid lvalue value for assignment error while trying to make a null pointer

I have a pointer to the type of structure that I created. When the program starts, it starts as NULL, and then malloc / realloc, since I need to add / remove these structures, and I'm just going to use my pointer to point to the first structure and navigate it like an array.

When I malloc / realloc, I always make the size of the "array" / region in memory larger than necessary. I do this, so I can set the “last index” / memory region to NULL so that I can say something like while (pointer! = NULL).

I get an error: invalid lvalue value in assignment when I try to assign NULL to the last position in an array / memory area using strings:

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

I think I say:

  • It's time to add a new structure to my array so that I increase connrhosts by one.
  • Rename the memory that is pointed to the remote system to a new memory area, which is the size of connrhosts (how many structures I will use), as well as one extra space so that I can make it NULL
  • Delete points to a new memory area
  • Use my pointer repeater and add connrhosts offsets that will now point to the last index of the memory region and make this pointer NULL.

As far as I can judge (or feel), I did everything right, but now I'm working on this project, and I get the impression that I have a tunnel vision. I would like a fresh set of eyes to look at my logic / code and make it clear what they think and what I did wrong. Thanks again: D

- - , , .

:

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;

, , / , NULL, - . , while (NULL!= Remotelist). , , , , ? , /, , ? , - while (NULL!= * (Remotelist + someoffset))?

, / .

/ : rhost * remotelist = NULL;

+3
5

lvalue , LHS , , . , , ( ), RHS.

:

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

, connrhosts - int size_t, :

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

.

*(remotelist + connrhosts) = NULL;

,

 remotelist[connrhosts] = NULL; 

.

+3

, , , *.

*(remotelist + connrhosts) = NULL;
+2

pointer != NULL , , . NULL, .

NULL, , , struct.

, , 0 :

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

- p->field == 0, 0 ...

, , , , , , , , - , realloc().

0

" /" NULL NULL. NULL .

[ ] , C89/90 = { 0 } initializer:

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

memset ( ).

, -... , memcmp

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

( , "", memset).

but usually this is not done. And there really is no point in doing it that way. Usually you should just select only one field in your structure ("primary"), which can tell you whether the structure is used: or not and compares only one field with 0

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

Source: https://habr.com/ru/post/1735828/


All Articles