简体   繁体   中英

First element in two dimensional array being overwritten - C

I've been having the same issue for a while now and I can't seem to get my head around it no matter how much research. I have came up with some theories why it may be happening though.

Basically, I'm writing a simple C shell and I'm encountering an annoying error when trying to implement aliases which I am going to store in a two-dimensional array. Whenever I try to assign more than one alias to the array, it overwrites the first element.

I thought it might be down to:

  1. Memory issues when tokenizing the input again
  2. Issues with array "decay" and pointers
  3. My compiler hates me.

Here is my code:

void fillArray(char* tokens[], char* aliasArray[ALIAS_NO][TOKEN_NUM]) {
    /* Integer for the for loop */
    int i;
    /* Counter for attribute addition */
    int counter = 2;
    /* Onto the search */
    for (i = 0; i < ALIAS_NO; i++) {
        if (aliasArray[i][0] == NULL) { /* If there is a space here */
            aliasArray[i][0] = tokens[counter-1]; /* Assign the alias */
            while (tokens[counter] != NULL) { /* While there is still stuff left */
                aliasArray[i][counter-1] = tokens[counter]; /* Add it in */
                counter++; /* Increment the counter */
            }
            return;
        }
    }
    return;
}

Where ALIAS_NO and TOKEN_NUM are preprocessor directives of the value 10 and 50 respectively.

The check works to see if the entry is null when I print the status of i and I also initialise every element in the multidimensional array to NULL.

Any assistance will be greatly appreciated. I've been banging my head against the wall for far too long now.

Thanks :)

EDIT: I've also tried to use the strcpy() function. Unfortunately, this throws a segmentation fault.

EDIT: New code

 void fillArray(char* tokens[], char* aliasArray[ALIAS_NO][TOKEN_NUM]) {
/* Integer for the for loop */
int i;
/* Counter for attribute addition */
int counter = 2;
/* Buffer */
char buffer[200];
/* Onto the search */
for(i = 0; i < ALIAS_NO; i++) {
    if(aliasArray[i][0] == NULL) { /* If there is a space here */
        strcpy(buffer, tokens[counter-1]);
        aliasArray[i][0] = buffer; /* Assign the alias */
        while (tokens[counter] != NULL) { /* While there is still stuff left */
            strcpy(buffer, tokens[counter]);
            aliasArray[i][counter-1] = buffer; /* Add it in */
            counter++; /* Increment the counter */
        }
        return;
    }
}
return;
}
for(i = 0; i < ALIAS_NO; i++)
{
    if(aliasArray[i][0] == NULL)
    {
        aliasArray[i][0] = strdup(tokens[counter-1]);
        while (tokens[counter] != NULL)
        {
            aliasArray[i][counter-1] = strdup(tokens[counter]);
            counter++;
        }
        break;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM