I’m experimenting with the concept of pointer to multi-dimension array in C. Suppose I want to process a multi-dimensional array via a function. The code kinda looks like this:
#include <stdio.h>
void proc_arr(int ***array)
{
// some code
}
int main(int argc, char **argv)
{
int array[10][10];
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
array[i][j] = i * j;
}
}
proc_arr(&array);
return 0;
}
The problem is, when I want to access array inside proc_arr, i can’t. From my understanding, we should access it this way:
void proc_arr(int ***array)
{
(*array)[0][1] = 10;
}
So I derefer the array to tell the compiler that I want to go to that address and get the value. But somehow, it crashes. I’ve tried several combinations of * and parentheses and still can’t make it work. I’m pretty sure it’s because of me not understanding pointers and pointers of pointers.
Oh, and I’ve noticed that it’s different if we work with a char **(array of string) too, like for argv and envp. But for envp, I somehow can access it with (*envp). Why?
Here’s the function that procces envp (and worked):
int envplen(char ***envp)
{
int count = 0;
while((*envp)[count] != NULL)
{
count++;
}
return count;
}
Also, can I somehow access envp in the envplen function with only envp, but still pass it by reference?
Thanks before.
The problem is because
int array[10][10]allocated on the stack does not lay out memory the way you think it does. This is because arrays are not pointers. The memory is still laid out in a linear array, not a “two dimensional” array, even though that’s what the subscripts might indicate. In other words, the memory forint array[10][10]looks like the following:So when you implicitly convert the array to an
int***, and then try to access the array like (*array)[1][10], what this actually translates to is something like*(*((*array) + 1) + 10), and the memory layout for such an operation wants to see memory setup like the following: