String parsing error in C "left operand must be l-value"

I ran into the need to pull information in a string of the format "blah.bleh.bloh" in ANSI C. Normally I would use strok () to execute this, but since I get this string through strtok and strtok are not thread safe, I cannot use this option.

I wrote a function to manually parse a string. Here is a snippet:

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 am trying to find the first appearance of '.' and pay attention to the character index. Then I convert '.' to zero char and copy the first line to the array.

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

Sorry, I get an error:

theString = &theString[charIndex + 1];

Mistake:

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

? ? , - .

EDIT: :

char theString[1024] = {0};

, , 1024 .

+3
7

, theString , . char , .

, ,

char theString[100];

- :

char *str = theString;

str , theString.

+8

strtok_r, . , , .

+4

C, , :-).

getNext(), , , NULL-. . , (, , unit test).

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *getNext (char *pStr, char *pComp) {
    /* Special for '.' at string end. */
    if ((*pStr == '.') && (*(pStr+1) == '\0')) {
        *pComp = '\0';
        return pStr + 1;
    }

    /* Check if no components left. */
    if (*pStr == '\0')
        return NULL;

    /* Transfer component one character at a time. */
    while ((*pStr != '\0') && (*pStr != '.'))
        *pComp++ = *pStr++;
    *pComp = '\0';

    /* Skip '.' at end, if there, but not at end of string. */
    if ((*pStr == '.') && (*(pStr+1) != '\0'))
        pStr++;

    // Return location of next component.
    return pStr;
}

int main (int argCount, char *argVal[]) {
    int argNum;
    int compNum;
    char *newStr;
    char *strPtr;

    if (argCount < 2) {
        printf ("Usage: components <string to componentize>...\n");
        return 1;
    }
    for (argNum = 1; argNum < argCount; argNum++) {
        if ((newStr = malloc (strlen (argVal[1]) + 1)) == NULL) {
            printf ("Out of memory for '%s'.", argVal[argNum]);
        } else {
            printf ("Input string is '%s'.\n", argVal[argNum]);
            compNum = 0;
            strPtr = getNext (argVal[argNum],newStr);
            while (strPtr != NULL) {
                printf ("   Component [%3d] is '%s'.\n", ++compNum, newStr);
                strPtr = getNext (strPtr,newStr);
            }
            free (newStr);
        }
    }

    return 0;
}

:

[fury]> components your.test.string .dot.at.start at.end. .both. no_dots ''
Input string is 'your.test.string'.
    Component [  1] is 'your'.
    Component [  2] is 'test'.
    Component [  3] is 'string'.
Input string is '.dot.at.start'.
    Component [  1] is ''.
    Component [  2] is 'dot'.
    Component [  3] is 'at'.
    Component [  4] is 'start'.
Input string is 'at.end.'.
    Component [  1] is 'at'.
    Component [  2] is 'end'.
    Component [  3] is ''.
Input string is '.both.'.
    Component [  1] is ''.
    Component [  2] is 'both'.
    Component [  3] is ''.
Input string is 'no_dots'.
    Component [  1] is 'no_dots'.
Input string is ''.
+2

"theString" , .

0

theString = &theString[charIndex + 1]; . , theString[charIndex] , , theString.

:

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

, osi_string_copy_n, - . , , , ?

Edit:
, charIndex++. theString ? , , wchar_t

0

libc, strtok_r, strtok.

char *saveptr;
char *str;
for (str = strtok_r(theString, ".", &saveptr);
        str;
        str = strtok_r(NULL, ".", &saveptr)
    )
{
    printf("got: '%s'\n", str);
}

, - strtok, strtok_r ( , saveptr).

0

, , . , theString #define d .

-1

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


All Articles