Author Topic: Back to C after a while. Got an lvalue problem :/  (Read 797 times)

0 Members and 1 Guest are viewing this topic.

Offline 0poitr

  • Peasant
  • *
  • Posts: 149
  • Cookies: 64
    • View Profile
Back to C after a while. Got an lvalue problem :/
« on: December 31, 2013, 02:04:43 pm »
Code: [Select]
struct split *stringSplit (char *str, char *delim)
{               
    char **token_array;
    *token_array = NULL;
    allocPtrs (token_array);
       
    char *token = str, *saveptr;
    unsigned int bulk = 1, j;

    for (j=0; ; token=NULL) {
        token = strtok_r (token, delim, &saveptr);
        if (token) {
            if (j == bulk*CHAR_PTRS) {
                allocPtrs (token_array);
                bulk++;
            }
            *token_array+j = calloc (1, strlen (token)+1);
            if (! *token_array+j )
                error ("calloc");
            strcpy (*token_array+j, token);
            j++;
        }
        else break;
    }

    struct split *s_arr = calloc (sizeof(struct split), 1);
    s_arr->num = j;
    s_arr->token_array = token_array;

    return s_arr;
}

Code: [Select]
stringlib.c: In function ‘stringSplit’:
stringlib.c:50:28: error: lvalue required as left operand of assignment
             *token_array+j = calloc (1, strlen (token)+1);
                            ^

The function is meant to split the string by the delimiters and return an array of substring tokens.
What's going wrong? GCC's considering *token_array+j as a constant. WHY! I'm starting to loose mental sanity  >:(
Imagination is the first step towards Creation.

Offline youpi

  • Serf
  • *
  • Posts: 20
  • Cookies: -6
    • View Profile
Re: Back to C after a while. Got an lvalue problem :/
« Reply #1 on: December 31, 2013, 06:19:00 pm »
Try to use () in your pointer+j

*(token_array+j) = calloc (1, strlen (token)+1);

Staff note: try to use the edit button next time.
« Last Edit: December 31, 2013, 07:30:02 pm by Kulverstukas »