
Untitled
By: a guest on
May 9th, 2012 | syntax:
None | size: 0.82 KB | hits: 16 | expires: Never
C pointer to pointer
int **p
int *p[1]
int **p;
int *p[1];
void foo()
{
int **ptr;
int *array[10];
sizeof(ptr); // just the size of the pointer
sizeof(array); // 10 * sizeof(int *)
// popular idiom for getting count of elements in array:
sizeof(array)/sizeof(array[0]);
}
// this would always discard the array size,
// because the argument always decays to a pointer
size_t my_sizeof(int *p) { return sizeof(p); }
int **p
int *p[1];
int **p
int *p[1]
int **pp; // pointer to pointer
int *ap[1]; // array of pointer
foo(ap);
pp = malloc(sizeof *pp * N); // allocate N pointers to int (type of *pp == int *)
if (pp)
{
size_t i;
for (i = 0; i < N; i++)
pp[i] = ...; // set pp[i] to point to some int value
}
ap = some_new_pointer_value();