Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please Explain the ambiguity in pointers in C?

Tags:

c

pointers

#include<stdio.h>
main()
{ int x[3][5]={{1,2,10,4,5},{6,7,1,9,10},{11,12,13,14,15}};


printf("%d\n",x); 
printf("%d\n",*x); }

Here first printf will print the address of first element. So why not the second printf prints the value at the address x i.e the first value. To print the value I need to write **x.

like image 901
dejavu Avatar asked Sep 08 '11 16:09

dejavu


2 Answers

For pointers, x[0] is the same as *x. It follows from this that *x[0] is the same as **x.

In *x[0]:

x is a int[3][5], which gets converted to int(*)[5] when used in expression. So x[0] is lvalue of type int[5] (the first 5-element "row"), which gets once again converted to int*, and dereferenced to its first element.

*x is evaluated along the same lines, except the first dereference is done with an asterisk (as opposed to indexing), and there is no second dereference, so we end up with lvalue of type int[5], which is passed to printf as a pointer to its first element.

like image 149
jpalecek Avatar answered Oct 25 '22 17:10

jpalecek


Arrays, when used as arguments to functions, decay into pointers to the first element of the array. That being said, the type of object that x decays into is a pointer to the first sub-array, which is a pointer to an array of int, or basically int (*)[5]. When you call printf("%d\n",*x), you are not feeding an integer value to printf, but rather a pointer to the first sub-array of x. Since that sub-array will also decay to a pointer to the first sub-array's element, you can do **x to dereference that subsequent pointer and get at the first element of the first sub-array of x. This is effectively the same thing as *x[0], which by operator precedence will index into the first sub-array of x, and then dereference the pointer to the first sub-array's element that the first sub-array will decay into.

like image 32
Jason Avatar answered Oct 25 '22 18:10

Jason