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...