1

Some says that array in c is not a pointer to the first element of the array why is array name a pointer to the first element of the array? So when you print the array in c using "printf" for example why it is showing the address of the first element instead of the array elements?

Update:

const char  h[10]="Hello";
printf("%p", h);

Output: 00AFFE0C

well as one answer said said that this happens because of the %type I specified and that made sense because when I write

printf("%s", h);
OR
printf(h);

Output: Hello

here two questions rises:

1) In printf(h); why it is not decaying the array and printing the pointer value because in decaying array will be converted to pointer

2) how can I print an array of int in the same way i.e. what is the %type in printf for array of int

Community
  • 1
  • 1
  • Show your code. – Hatted Rooster May 10 '17 at 16:53
  • 2
    Some good reading: http://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer-in-c – NathanOliver May 10 '17 at 16:53
  • Can you show an example of how you print an array with expected and actual output? – dbush May 10 '17 at 16:53
  • When an array variable is used in an expression, it some contexts it decays to a pointer to the first element but in some contexts it does not. – R Sahu May 10 '17 at 16:54
  • 2
    Because once you `print` it, you pass it to a function. Once array is passed to a function, it is decaying to a pointer. This is the way it works. – Eugene Sh. May 10 '17 at 16:54
  • Same thing happens also here ptr = arr. where ptr is a pointer and arr is an Array – Michi May 10 '17 at 16:57
  • what do you mean by 'address of array elements' ?. Consider an array of 10 elements , then 'address of array elements' means the address of all 10 elements. How will that be possible. – GAURANG VYAS May 10 '17 at 16:59
  • 1
    That depends....on how you want the output to be formatted....read the man page of printf() first... – Sourav Ghosh May 10 '17 at 16:59
  • Probably you mean Array names and not Array itself, when you ask `Why array holds the address of the first element in C? ` If not, you could think that this `int arr[] = {1,2,3}; int *ptr; ptr = &arr;` is OK. – Michi May 10 '17 at 17:01

1 Answers1

0

The difference between an array and a pointer is that the compiler knows about the array size and dimensions so it can perform some static checks address computations that are not possible with just pointers.

Additionally in the case of printf() it doesn't matter what you pass it because it's a variadic function. It doesn't know anything about the type of the arguments that you pass it except from the format string so if you pass a %s argument you're in effect casting it to char*. The only reason the compiler may give you a warning if you pass an argument of the wrong type is because it's an intrinsic function and the compiler knows about it.

Most functions that take arrays take them as pointer arguments so you're also implicitly casting the array to a pointer when calling a function unless the function specifies an array type as argument.

fernan
  • 349
  • 1
  • 6