1

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:

enter image description here

Example with type int 2D array:

enter image description here

9
  • 1
    int *arr[] is not a 2D array, it is an array-of-pointers-to-int. int arr[x][y] with x & y are integer literals creates a 2D array. (which on access will be converted to a pointer to the first element (which is a pointer the first 1D array, e.g. a pointer to array of int[y]) which is not the same as int** (which is simply a pointer to pointer to int). Commented Nov 6, 2019 at 6:44
  • 1
    type *arr[] is an array of pointers. What follows, e.g. char *arr[] = {"one", "two"}; must be pointers. For int *arr[]; you would need for instance int a[] = {1, 2, 3); and int b[] = {3, 4, 5};, then int *arr[] = {a, b}; as both a and b will be converted to pointers on access. C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3). Also a 2D array is actually an array of 1D arrays. You can represent as int arr[x][y]; or int (*arr)[y]; So type (*arr)[y]; is a pointer to array. Commented Nov 6, 2019 at 7:03
  • 1
    This answer may help further Converting array of pointers to double pointer. There are many more duplicates, some better than others, unfortunately the SO "Related" links on the right side of the page do not do a good job finding them. Commented Nov 6, 2019 at 7:10
  • 1
    Have a look at what is the difference between *pt in int (*pt)[2] and pt in int *pt?, the answers there may be more instructive. There are still more complete answers I'm looking for, but that question is good. Commented Nov 6, 2019 at 7:17
  • 1
    Here is one more that will help (read the comments too) Processing the columns of a multidimensional array in C. You can find many more questions and answers, but there isn't one specific search likely to find them all given how different problems are described in the original title of the question. The problems all boil down to understanding how array / pointer conversion works. That's section 6.3.2.1 linked earlier. It's worth getting a handle on now, it will save you hours later. Commented Nov 6, 2019 at 7:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.