Because I followed a discussion where was told "Aliasing through incompatible pointer types is undefined behavior" (e.g. double d; int *p = (int *)&d;
following question:
Is it allowed to cast an (double *)
to (double **)
, e.g. double *d1; double **d2 = &d2
and using syntax like d2[0][y]
expecting to be the same as d1[y]
?
I know that it is not exactly aliasing through incompatible pointer types, but however I am not sure. Background is that I want to have a function which operates on 2-dimensional arrays (= images) but I want to be able to pass only a row or column of an image.
double**
is incompatible withdouble*
. The conversion is legal, but the only thing you can do with the results is cast it back. This seems more or less obvious: on a 32 bit machine, adouble*
won't even have the same size as adouble
.But your example doesn't convert a
double*
todouble**
. It creates a newdouble**
, which points to thedouble*
. This is fine.