Tuesday, 15 April 2014

c++ - array index and address return same value -



c++ - array index and address return same value -

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

now don't why , &a homecoming same value, reasoning , practical application behind it? type of &a , &(&a) ?

now don't why , &a homecoming same value, reasoning

a name of array decays pointer first element of array. &a nil address of array itself, although a , &a print same value types different.

also type of &a?

pointer array containing 3 ints , i.e int (*)[3]

could &(&a) ?

no, address of operator requires operand lvalue. array name non-modifiable lvalue &a legal &(&a) 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 printing address(using wrong format specifier in printf invokes undefined behavior)

c++ c arrays pointers

No comments:

Post a Comment