Home > Back-end >  Why do the pointers in my array point to the same string?
Why do the pointers in my array point to the same string?

Time:09-23

I'm trying to understand why an issue is happening. I have a file, from which I read several lines:

char *array_slave[128];

int i = 0;
while ((read = getline(&line, &len, fp)) != -1)
{
    if (strstr(line, "X") != NULL)
    {
        array_slave[i] = line;
        printf("%s\n",array_slave[i]);
        i  ;
    }
}

After this cycle, I know that array_slave contains 32 lines:

size_t array_length(char *ptr[])
{
    size_t i=0;
    while(ptr[i]!=NULL){
        //printf("%d\n", i);
        //printf("%s\n",ptr[i]);
        i  ;
    }
    return i;

}

Now, I simply want to print the last 4 elements of array_slave. Anyway, I noticed that it prints always the same line:

for(int i=0; i<10;i  ){
    printf("%s\n", array_slave[i]);
}

I think that this happens because, in the first cycle, the i operation shifts the pointer, so now it is in a memory address that is not of my interest. How can I return the original position of array_slave? I want to point to array_slave[0], array_slave[1] and so on...

CodePudding user response:

Because they are both pointing to same line. array_slave[i] = line; stores the same address at the ith location for each i. The memory pointed by line is overwritten with new content each time. You should copy the contents of line to a new place and store the address of that place in the array_slave array.

CodePudding user response:

Alternatively to the solution proposed by the previous answers the problem could also be solved by setting line to NULL after copying the pointer to slave_array:

char *array_slave[128];

int i = 0;
while ((read = getline(&line, &len, fp)) != -1)
{
    if (strstr(line, "X") != NULL)
    {
        array_slave[i] = line;
        line = NULL;  // HERE
        printf("%s\n",array_slave[i]);
        i  ;
    }
}

That will cause getline() to malloc() a new buffer when it's called the next time. Be aware you have to free() each array_slave[i] when you don't need it anymore.

CodePudding user response:

After this cicle, I know that array_slave contains 32 lines

That is not entirely accurate, it contains 32 pointers to char that will all point to1 the address of line, which is in itself a pointer to char that will be pointing to a unique memory location, it will not change where it's pointing to along the loop, what will change is what is stored in that memory location, which is the read line in that particular iteration.


So with the above information you can see that the pointers stored in array_slave will all point to line and print whatever is in there, repeatedly in your cycle, and what line contains2 is the last line you read from the file.

What you need to do is to copy the content of line to array_slave[i] in each iteration:

//...
if (strstr(line, "X") != NULL)
{
    strcpy(array_slave[i], line); //<---
    printf("%s\n",array_slave[i]);
    i  ;
}
//...

1 - Contain the address of
2 - Points to

  • Related