For starters to output a pointer you need to use the conversion specifier %p
instead pf the conversion specifier %d
. Otherwise the call of printf
invokes undefined behavior.
printf("address of ch is %p\n", ( void * )ch);
Secondly within the function this call of printf
does not output the address of the variable ch
itself. It outputs the address of the first element of the array pointed to by the pointer expression ch
. As the array is not moved within memory then you will get the same value of the address.
To output the address of the function parameter you need to write
void print(char ch[]) {
printf("address of ch is %p\n", ( void * )&ch);
}
Pay attention to that in this call
print(ch);
the array is implicitly converted to pointer to its first element. And the compiler adjusts the function parameter having an array type to pointer to the array element type
void print(char *ch) {
There is a difference between these two calls of printf
in main where the source array is declared and within the function
printf("address of ch is %p\n", ( void * )ch);
printf("address of ch is %p\n", ( void * )&ch);
Within the function the first call outputs the address of the first element of the array pointed to by the pointer ch
that is the address of the extent of memory occupied by the array.
The second call outputs the address of the local variable ch
itself having the type char *
(as pointed above).
In main these calls outputs the same value that is the address of the extent of memory occupied by the array. The first call outputs the address of the first element of the array and the second call outputs the address of the array as a whole object. The both values are the initial address of the extent of memory occupied by the array.