1

Possible Duplicate:
Is 2d array a double pointer?

void fun(int **ptr,int n)
{
int i=0;j=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
 printf("%d  ",a[i][j]);

}

Int main()
{
int arr[20][20];
int **ptr=arr;  //Statement 1
fun(arr,20);
}

Why does statement 1 give a warning and the function call doesn't? I saw that this is an exceptional case in function calls. Is there a reason behind this? How does the 'arr' a pointer to an array becomes a double pointer 'ptr' and still we can use it like a pointer to an array? Thanks in advance.

6
  • 1
    Please do more research, there are literally thousands of SO questions/textbooks/articles explaining this. Start with K&R The C Programming Language. Commented Nov 29, 2012 at 17:13
  • 2
    I don't recall there being an Int in C. Apart from that, your program has several syntax errors. It doesn't compile. Commented Nov 29, 2012 at 17:14
  • 1
    This C++ FAQ is really useful to understand arrays and their relationship with pointers, ASCII-art is really helpful, just consider only the C part. Commented Nov 29, 2012 at 17:18
  • 1
    For a question like this it is essential that you provide a compilable example code. Really. It should be necessary for answers to write "It does, after fixing the code so that it compiles". More over you should have exhibited the the output of the compilation and said what platform you were on. Commented Nov 29, 2012 at 17:20
  • 2
    And this is a frequently asked question in C. Literally. Here's the C-FAQ entry: c-faq.com/aryptr/pass2dary.html Commented Nov 29, 2012 at 17:21

2 Answers 2

2

Because int ** and int (*)[20] are two fundamentally different types. For the latter the size of the array it points to is fixed at compile time.

When you dereference an int ** you expect to find a int* - pointer which points to an int. When you dereference a int (*)[20] you expect to find a block of 20 ints, not more pointers.

When you allocate int arr[20][20] then arr is a compile time symbol equivalent to int (*)[20], not int **. If you wanted the latter you would have to allocate a int * arr[20] and then populate the pointers to another block of memory containing int [20].

Sign up to request clarification or add additional context in comments.

Comments

0

Why does statement 1 give a warning and the function call doesn't?

It does, after fixing the code so that it compiles:

danger.c: In function ‘main’:
danger.c:15:11: warning: initialization from incompatible pointer type [enabled by default]
danger.c:16:1: warning: passing argument 1 of ‘fun’ from incompatible pointer type [enabled by default]
danger.c:3:6: note: expected ‘int **’ but argument is of type ‘int (*)[20]’

and the reason for the warning is well explained (should be an error, IMO, but gcc is very permissive). You are passing an incompatible pointer type. And that warning comes even with default warning level.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.