If I have a 2d array B defined as :
int B[2][3] = {{1,3,5},{2,4,6}};Is
int **p = Bsame asint (*p)[3] = B?int **f = B; printf("%d ",*f+1);gives
5as output whileprintf("%d ",*f)gives 1 as answer. Why is that happening?printf("%d ",**f);returns a segmentation fault! Why?
- 1,154
- 3
- 16
- 31
-
yeah! its a typo. Corrected it – Dubby Aug 01 '14 at 12:42
-
That's a pointer to a 1D array, not a pointer to a 2D array. – chris Aug 01 '14 at 12:51
-
[This](http://stackoverflow.com/a/17953693/1237747) may be a bit helpful. – ST3 Aug 01 '14 at 13:07
2 Answers
No.
int **p = B;is an error. (Both a compilation error, and a logical error). Anint **must point to anint *. However, there are noint *stored inB.Bis a group of contiguousints with no pointers involved.int **f = B;must give a compilation error. The behaviour of any executable generated as a result is completely undefined.See 2.
To explain why you might be seeing 1 and 5. (The C standard does not define this, but your compiler bulls on ahead anyway). Probably your compiler treats the line as
int **f = (int **)B;
Then the expression *f will read bytes from the storage of B (which actually hold ints) and pretend that those are the bytes that make up a pointer representation. This is further undefined behaviour (violation of strict-aliasing rules). Probably the result of this is that *f is a pointer to address 0x00000001.
Then you print a pointer by using %d, causing further undefined behaviour. You see 1 because your system uses the same method for passing int to printf as it does to pass int *.
When you add 1 to (int *)0x00000001, you get (int *)0x00000005, because incrementing a pointer means to point to the next element of that type.
When you dereference this pointer, it causes a segfault because that address is outside of your valid address space.
- 138,810
- 21
- 208
- 365
-
on C99 it just gives a warning `warning: initialization from incompatible pointer type [enabled by default]` – Dubby Aug 01 '14 at 12:40
-
@Dubby the technical term is *a diagnostic*, and the program is ill-formed. It's up to the individual compiler if it wants to proceed after that (and the behaviour has left the realm of what is covered by the C standard) – M.M Aug 01 '14 at 12:42
-
So basically a declaration like `char *argv[]` is not a 2 dimensional array i.e. not the same as `char argv[m][n]` ? – Dubby Aug 01 '14 at 12:43
-
-
No, that's a function parameter and the meaning of those is different to variable declarations. [see here](http://stackoverflow.com/questions/22677415/why-do-c-and-c-compilers-allow-array-lengths-in-function-signatures-when-they/) for details. – M.M Aug 01 '14 at 12:45
1) Is int **p = b same as int (*p)[3] = b ? - No. int **p = b is an error.
Because here int **p is a pointer to pointer to an integer, but int (*p)[3] is pointer to an array of 3 integers!
2) int **f = B; It is an error, May results in Undefined behavior!
3) printf("%d ",**f); - It is same as (2). int **f = B; is error, so Undefined behavior!
NOTE: To avoid this type of error enable some warning flags in compiler option and try!
- 3,740
- 1
- 17
- 28