constant multidimensional array in a function argument

S

Szabolcs Borsanyi

The following code compiles with a warning message:
passing arg 1 of `use_vector' from incompatible pointer type

How can one correctly pass a multidimensional array (by reference)
expressing that 'use_vector'
is not meant to modify any of the elements.

Thanks
Szabolcs

------------------------------------------------------------
enum { E=3 };
typedef double vector[4];

void use_vector(const vector x[E])
{
}

int main()
{
vector v[E]={0};
use_vector(v);
}
 
B

Ben Bacarisse

Szabolcs Borsanyi said:
The following code compiles with a warning message:
passing arg 1 of `use_vector' from incompatible pointer type

Yes, you have hit a small hole in the way C handles qualified types.
How can one correctly pass a multidimensional array (by reference)
expressing that 'use_vector'
is not meant to modify any of the elements.

Pretty much all you can do is use a cast:

use_vector((const vector *)v);

(or drop the const from the parameter).
enum { E=3 };
typedef double vector[4];

void use_vector(const vector x[E])
{
}

int main()
{
vector v[E]={0};
use_vector(v);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top