I am experimenting with the concept of a pointer to a multidimensional array in C. Suppose I want to process a multidimensional array through a function. The code is as follows:
#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 that when I want to access the array inside proc_arr , I cannot. In my opinion, we should access it this way:
void proc_arr(int ***array) { (*array)[0][1] = 10; }
So, I canceled the array to tell the compiler that I want to go to this address and get the value. But for some reason he falls. I tried several combinations of * and parentheses and still can't get it to work. I am sure that this is because I do not understand pointers and pointer pointers.
Oh, and I noticed that this is different if we work with char ** (a string array) as well as for argv and envp. But for envp, somehow I can access it using (*envp) . Why?
Here is the function that executes envp (and works):
int envplen(char ***envp) { int count = 0; while((*envp)[count] != NULL) { count++; } return count; }
Also, can I somehow access envp in the envplen function only with envp , but still pass it by reference?
Thanks before.
source share