No. int **p = B; - mistake. (Both a compilation error and a logical error). int ** must point to int * . However, there is no int * in B B is a group of contiguous int without pointers.
int **f = B; should give a compilation error. The behavior of any executable file generated as a result is completely undefined.
See 2.
To explain why you can see 1 and 5 . (The C standard does not define this, but your bull compiler is ahead in any case). Your compiler probably treats the line as
int **f = (int **)B;
Then the expression *f will read the bytes from the storage B (which actually hold int s) and pretend that these are the bytes that make up the representation of the pointer. This is also undefined behavior (violation of strict anti-aliasing rules). Probably the result of this is that *f is a pointer to the address 0x00000001 .
Then you print the pointer using %d , causing further undefined behavior. You see 1 because your system uses the same method to pass int to printf , as it does for passing int * .
When you add 1 to (int *)0x00000001 , you get (int *)0x00000005 , because incrementing a pointer means pointing to the next element of this type.
When dereferencing this pointer, it calls segfault because this address is outside the valid address space.
MM Aug 01 '14 at 12:39 on 2014-08-01 12:39
source share