I have an array of pointers to arrays, in a function. I need to access that array in another function.
Pointer array
char *ptr[size + 1];
I made a global pointer
char *p;
which i pointed to ptr (in the same function as ptr)
p = *ptr;
then i tried to access ptr from the other function
void otherFunction(void){
for(int n = 0; n < 6; n++)
{
char* str = &p[n];
printf("%i %s\n", n, str);
}
}
when
ptr[0] = "abcd"
ptr[1] = "bcde"
ptr[2] = "cdef"
ptr[3] = "defg"
ptr[4] = "efgh"
ptr[5] = "fghi"
the output of otherfunction(); is:
0 abcd
1 bcd
2 cd
3 d
4
5
I want the output to be
0 abcd
1 bcde
2 cdef
3 defg
4 efgh
5 fghi
My questions are: (0) whats going wrong here. (1) How do i fix it (2) Or is there a better way to do this. the requirements are otherfunction() cant take any arguments and ptr needs to remain local to its function. (Im certain the other code is not contributing to the problem and nothing is wrong with ptr)
p = *ptr;dereferencesptrand is identical to sayingp = ptr[0];p = ptr;would be an error