I have a problem.

I wrote:

for(i=0; i<n; i++)
        char **exP = realloc(exParam, m*sizeof(*exParam));
        char **exT = realloc(exText, m*sizeof(*exText));
        exParam = exP;
        exText = exT;

        explode = strtok(lines[i], "#");
        while(explode != NULL)
            exParam[m-1] = malloc(strlen(explode+1));
            strcpy(exParam[m-1], lines[i]);
            exText[m-1] = malloc(strlen(explode+1));
            strcpy(exText[m-1], explode);
            explode = strtok(NULL, "#");

            //printf("> %s\n", explode);
            //printf("> %s\n", exParam[m-1]);
            //printf("> %s\n", exText[m-1]);


And I want to obtain two arrays. My first array (lines) looks like SOMETHING#Some text. How to explode this text to two arrays exParam with data before # and exText with string after #? What's wrong in my method?

6 Years
Discussion Span
Last Post by Narue

Without error handling:

char **exParam = malloc(n * sizeof exParam);
char **exText = malloc(n * sizeof exText);

for (i = 0; i < n; i++) {
    char *param = strtok(lines[i], "#");
    char *text = strtok(NULL, "#");

    exParam[i] = malloc(strlen(param) + 1);
    exText[i] = malloc(strlen(text) + 1);

    strcpy(exParam[i], param);
    strcpy(exText[i], text);

Obviously you want to make sure that strtok doesn't return NULL, and that all dynamic allocations succeed before doing any writing.

What's wrong in my method?

Are you getting compile-time errors? Runtime errors? I see only one syntax error which most likely isn't a problem if you're accidentally compiling as C++ (or less likely as C99).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.