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

Now i don't get why a and &a return the same value, what is the reasoning and the practical application behind it? Also what is the type of &a and could i also do &(&a) ?

1 Answer 1

10

Now i don't get why a and &a return the same value, what is the reasoning

a is the name of the array that decays to pointer to the first element of the array. &a is nothing but the address of the array itself, although a and &a print the same value their types are different.

Also what is the type of &a?

Pointer to an array containing three ints , i.e int (*)[3]

could i also do &(&a) ?

No, address of operator requires its operand to be an lvalue. An array name is a non-modifiable lvalue so &a is legal but &(&a) is not.

Printing type of &a(C++ Only)

#include <typeinfo>
#include <iostream>

int main()
{
   int a[]={1,2,3};
   std::cout<< typeid(&a).name();
}

P.S:

Use %p format specifier for printing address(using incorrect format specifier in printf invokes Undefined Behavior)

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

7 Comments

but i don't get how &a is int (*)[3], i mean it is not that we declared it anywhere to be of this type.
Yeah, but why would i use c++ to tell me the type if i am working on C. Isn't there any other reason why the type is int (*)[3]? Or is it something that just makes sense, thinking about it logically?
@n0nChun : For an array of n elements of type T, the address of the first element has type ‘pointer to T’; the address of the whole array has type ‘pointer to array of n elements of type T’.
Thanks. And i found this too : ) publications.gbdirect.co.uk/c_book/chapter5/…
@n0nChun: If you say int i; you did not declare i as a pointer to int, yet &i is a pointer to int ;)
|

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.