c - Pointer-to-pointers addresses & arithmetic -


programmers!

i totally sunk double pointer (pointer-to-pointers)... many questions here!

let's start task: writing custom version of 'calloc' func, must return pointer 'n' memory elements of size 'size'. here invented:

void **calloc1(int n, int size) {     int i, j;     char *tmp, **p = null;     tmp = (char *) malloc1(n * size);     p = tmp;     (i = 0; < n; i++) {         p[i] = tmp;         (j = 0; j < size; j++)             tmp++;     }     p = &p[0];     return (void **)p; } /* entered n==4, size==3;  real pointers are: p[0] == 0x804cfe0; p[1] == 0x804cfe3; p[2] == 0x804cfe6; ... */ 

so, allocate n*size bytes, , "assign" array of pointers of equal 'size' corresponding start positions. let's enter n=4 , size=3; means p[0] points tmp[0], p[1] tmp[3], p[2] tmp[6] , on. in gdb, track pointers values after each step.

then, in 'main' declare double pointer , "attach" buffer received 'calloc':

int main (short argc, char **argv)     {         char **space;         space = (char **) calloc1(n, size);   /* removing '(char**)' here not have effect */       /*  @ stage pointers seems correctly "located": 'space' == 'space[0]' == 0x804cfe0; 'space[1]' == 0x804cfe3; 'space[2]' == 0x804cfe6; ... */ 

1) here first question: how can 'main()' (or other function pass copy of **p) know size of pointer arithmetic? example, how 'main()' knows if add '1' 'space' (or increment once), should point second pointer (in 'calloc' it's p[1]), (in particular case) 3 chars further of first pointer (p[0])? moreover, if create in 'alloc' array of pointers strings "variable length" (for example, p[0] points tmp[0], p[1] tmp[7], p[2] tmp[11] etc.), how other function know should increment "upper" pointer 4 , 7 "chars"?

alright, move further, try put chars acquired buffer:

int = 0, j = 0, n, size; char nn, ssize, c, temp[3];  printf ("enter number/size \n"); sc = scanf ("%c/%c", &nn, &ssize); n = nn - '0';  /* n==4 */ size = ssize - '0';   /* size==3 */   printf ("enter 'number' of words\n"); while (j < n) {     (i = 0; (c = getchar()) != eof && < size; i++)         *(*space)++ = c;      (*space)--;   /* line unneccesary; if remove - nothing changes */     ++j;     ++space; } 

2) , here evidence first question: actually, when increment 'space' here, moves not 3 4 chars (after first '++' 0x804cfe4, after second 0x804cfe8). why? there connection 'float'-type size? after first such incrementing, '*space' points 0x804cfe6... not think correct.

i have tried way - refering 'space' not pointer array:

.... while (j < n) {     (i = 0; (c = getchar()) != eof && < size; i++)         *space[j]++ = c;      space[j]--;     ++j; } 

3) in case, pointers seems ok - e.g. space[1] == 0x804cfe3, space[2] == 0x804cfe6. problem is, while loop operating j == 2, value of 'space[0]' somehow changes 0x804cfe2 (moved twice - ok) 0x6a04cfe2 (which out of bounds). h.. ???

4) , @ all, there weird behavior of addresses. have tried not write chars directly **space, use string copy function:

    char i, temp[3];       ...     while (j < n) {         (i = 0; (c = getchar()) != eof && < size; i++)             temp[i] = c;          strncpy1 (space[j],temp,3);         ++j;     } ..... void strncpy1 (char *s, char *t, int k) {     while (--k > 0) {         *s = *t;         s++;  t++;     } } 

inside copy func, copying , incrementing show in gdb correct. after returning 'strncpy1', space[j] changes 0x804cfe0 0x804000a. how possible called function able affect parents' (external) pointer?

so finally, type pointer-to-char-pointers? size have?

i believe on system size of pointer 4, , since using pointer pointer , incrmeneting adds 4 byte current location reach next pointer of type(char**).

note: not incrementing pointer char, incrementing pointer pointer.

now again if talk second question how function know increase 4 , 7 not related function. because array of pointer of pointer address saved on consecutive location(they not value of pointer, talking pointers save in array of pointer) increment pointer 1 , reach next pointer of type weather on p[0] or p[4] or p[7]..


Comments