Hi I am trying to understand the difference between passing a 2d array of type char
VS type int
to a function as a double-pointer func(int **ptr)
parameter. The problem is that it behaves differently. Below I will show two examples for type char
and int
using PythonTutor for C.
I looked at the whole internet and asked some friends but couldn't find an answer to the behavior.
Example with type char: int func(char **arr)
{
printf("%d", **arr);
}
int main()
{
char *arr[] = {"char1","char2"};
return 0;
}
Example with type int:
int func(int **arr)
{
printf("%d", **arr);
}
int main()
{
int *arr[] = {{1,3},{4,5,6}};
return 0;
}
One thing to note is that is not important what the func body is doing with the double-pointer passed to it, the problem i am trying to understand is why the double-pointer in the case of 2D char array is pointing to a pointer to integer while the 2D int array is not pointing to the integers within the arrays?
Below is the visualization from pythontutor for both cases.
Example with type char 2D array: http://pythontutor.com/c.html#mode=display
Example with type int 2D array: http://pythontutor.com/c.html#mode=display
Also provide snapshot of the final result:
Example with type char 2D array:
Example with type int 2D array: