Invalid lvalue in assignment error when trying to make a pointer NULL
c, compiler-construction, compiler-errors, pointers
Solution
Errornous lvalue assignments occur when the LHS is an evaluated expression that does not become a variable that can be assigned. What you're doing looks like an operation (pointer arithmetic) which should be on the 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:
remotelist += connrhost; // pointer arithmetic
*remotelist = NULL; // assuming remotelist is an array of pointers.
Problem
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. When I malloc/realloc I always make the size of the "array"/area in memory one larger than it needs to be. I do this so I can set the "last index"/area in memory to NULL so I can say something like while (pointer != 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: ``` // 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. - 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 - Point remotelist to the new area of memory - 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. 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 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. So my original loop statement is 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))? 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;