0

when i tried to understand more about how array name is treated as pointer, I wrote this code :

#include<stdio.h> 
   
int main() 
{ 
  int a[] = {1, 2, 3, 4, 5, 6}; 
  printf("%d \n",*(&a+1));
  printf("%d \n",**(&a+1));
  return 0; 
}

and the output is

6422304
6422284

I expect the first output will be address and the second will be value stored in address, Not two output is addresses

2
  • What are you intending with **(&a + 1)? Using more sensible notation like *a[1]. Commented Feb 15, 2023 at 16:02
  • Start with simpler things. What will be printed for passing a to printf vs &a ? Can you explain this? Commented Feb 15, 2023 at 16:05

2 Answers 2

3

You declared an array of 6 integers

int a[] = {1, 2, 3, 4, 5, 6}; 

That is the array has the type int[6].

The expression &a has the pointer type int ( * )[6]. And the expression &a+1 points to the memory after the array. So dereferencing the array like *(&a+1) you will get an expression of the array type int[6] that in turn is implicitly converted to a pointer of the type int *,

And in this call of printf

printf("%d \n",*(&a+1));

you are trying to output the pointer using the conversion specifier %d that invokes undefined behavior.

Instead you could write

printf("%p \n",( void * )*(&a+1));

and the value of the pointer expression (that points to the memory after the array) will be outputted.

In this call of printf

printf("%d \n",**(&a+1));

you are dereferencing the expression *(&a+1) explained above. As the value of the obtained pointer expression points beyond the array then dereferencing it invokes undefined behavior.

Instead of these two calls you could write for example using the expression &a

printf("%p \n",( void * )*(&a));

to output the address of the first element of the array and'

printf("%d \n",**(&a));

to output the value of the first element of the array.

Though it would be simpler to write

printf("%p \n",( void * )a );

and

printf("%d \n",*a );

And you will get the same result.

Or to output for example the address and the value of the second element of the array you could write

printf("%p \n",( void * )( a  + 1 ));

and

printf("%d \n",*(a + 1 ) );

The last call of printf is equivalent to

printf("%d \n",a[1] ) );

Pay attention to that array designators used in expressions with rare exceptions are converted to pointers to their first elements.

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

Comments

0

a is an array, which is also a pointer to a block of memory. So you can use *a to access the first value of the array, and *(a + 1) to access the next.

*(a + 1) works because the compiler knows the type of a is "pointer to integer". So adding 1 to it will give the address of the integer immediately after it.

3 Comments

"a is an array, which is also a pointer to a block of memory." - arrays and pointers are two different entities.:)
arrays and pointers are treated differently by the compiler, yes. But we can think of arrays as pointers to a block of memory, right?
Strictly speaking it is not correct. Arrays in expressions with rare exceptions are indeed converted to pointers to their first elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.