array index and address return same value

后端 未结 1 1926
慢半拍i
慢半拍i 2020-12-06 17:58
#include
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 g

相关标签:
1条回答
  • 2020-12-06 18:45

    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)

    0 讨论(0)
提交回复
热议问题