I am getting confused as to whether it is valid (in C) to pass a pointer to an array that has been initiated as follows (e.g. at compile time on the stack):
int a[] = {1, 2, 3};
my_func(&a);
void my_func(int **a)
{
int *tmp = *a; /* this statement fixes the problem */
printf("%d %d %d", (*a)[0], (*a)[1], (*a)[2]); /*doesn't work */
printf("%d %d %d", tmp[0], tmp[1], tmp[2]); /*does work */
}
when I step through this with gdb I can't 'see' any of the values (*a)[0], etc. from 'inside' my_func. e.g.
(gdb) p (*a)[0]
Cannot access memory at address 0x0
I'm thinking that possibly I have a fundamental misunderstanding with regard to what you can and can't do with arrays that are on the stack rather than the heap?
I hope thats not the case as it is very convenient for my unit tests to declare arrays on the stack as in the example, but I need to test functions that are expecting pointers to pointers to int.
Note I do get a compiler warning as follows:
test_utility.c:499:5: warning: passing argument 1 of ‘int_array_unique’ from incompatible pointer type [enabled by default]
../src/glamdring2.h:152:5: note: expected ‘int **’ but argument is of type ‘int (*)[12]’
but I thought it would be ok to mix int *a[] with **a? Perhaps not? Are they not equivalent?
ptr_int = (int)ptrfor storage ofptr_intandptr = (int*)ptr_intfor usage. But i'm not sure whether this is sufficient for your problem.