Ad

Error Parsing String In C " Left Operand Must Be L-value"

- 1 answer

I am faced with the need to pull out the information in a string of the format "blah.bleh.bloh" in ANSI C. Normally I would use strok() to accomplish this, but since I am getting this string via strtok, and strtok is not thread-safe, I cannot use this option.

I have written a function to manually parse the string. Here is a snippit:

for(charIndex=0; charIndex < (char)strlen(theString); charIndex++)
{
    if(theString[charIndex] == '.')
    {
        theString[charIndex] = '\0';
        osi_string_copy_n(Info[currentInfoIndex], 1024, theString, charIndex + 1 );
        currentInfoIndex++;
        theString = &theString[charIndex + 1];
    }
    charIndex++;
}

As you can see, I try to find the first occurrence of '.' and make note of the index of the character. Then I convert the '.' to a null char and copy the first string to an array.

Then I want to change the pointer to start just after where the delimiter was found, essentially giving me a new shorter string.

Unfortunately I am getting an error on the line:

theString = &theString[charIndex + 1];

The error is:

error C2106: '=' : left operand must be l-value

Why am I not allowed to move the pointer like this? Is my method flawed? Perhaps someone has a better idea for me to parse this string.

EDIT: In response to the comments, the declaration for theString is:

char theString[1024] = {0};

Also, I am guaranteed that theString will never be more than 1024 characters.

Ad

Answer

Under the assumption that you defined theString as an array, try defining it as a pointer. When you declare a char variable as an array, you cannot later change its address.

I am assuming you have a declaration similar to

char theString[100];

The easiest solution is to leave that declaration alone, and add another one:

char *str = theString;

and then use str everywhere that you currently use theString.

Ad
source: stackoverflow.com
Ad