I’m trying to write a glue function between two data types and I can’t seem to get the compiler to be happy. On one side, I have a pointer to a chunk of data that is logically a n x 2 array, but is declared as:
double* pData=new double[2*n];
On the other side, I have a c function that is declared as
void Function(double data[][2], int n);
If I remember my c syntax, the data[][2] is really just a pointer to a contiguous chunk of memory, but the compiler knows the size of the second dimension is 2. So I’d like to take pData and pass it into Function(), without a memcpy. I just can’t seem to write the cast. I thought something like
Function((double [][2])pData,n)
would work, but the compiler (MSVC 8) doesn’t like that. Can anyone let me know the proper way to write the cast to get the compiler to be happy.
Function parameters of the form
T[]are identical toT*(not evenT* constthat some people expect). This is a special case for parameter types in both C and C++. So yourdouble[][2]follows this rule, with T beingdouble[2]. Typedefs help illustrate this:So you write
T*when T isdouble[2]asdouble (*)[2].You could also do this:
Which requires no cast because pData is already the correct type. Or with typedefs: