Zahy
Zahy

Reputation: 377

Casting void* to two-dimentional array

I have a void* I am getting in some function which is actually a two-dimensional int array. I want to send it as an argument to a function that expects a two dimensional array. What is the BEST way to cast it properly?

void foo(void* val){
   //How to cast val in order to send to bar??
   bar()
}

void bar(int val[2][2]){
//Do something 
}

Upvotes: 3

Views: 2789

Answers (1)

ruakh
ruakh

Reputation: 183371

bar((int(*)[2]) val);

(As Carl Norum states, the cast isn't even actually required; but it has the advantage of giving you a compiler warning if you accidentally pass it to a function expecting, say, a int(*)[3].)

Upvotes: 7

Related Questions