Passing a char pointer to a function accept a char reference of size?

  • A+

I'm not quite sure if I titled the question correctly, but essentially, I have this scenario:

I'm trying to call this method

#define SIZE 16 void DoSomething(char(&value)[SIZE]) { } 

From this method:

void BeforeDoingSomething(char* value, int len) {     if (len == SIZE)     {         DoSomething(value);     } } 

Attempting to do this gives me this error:

a reference of type "char (&)[16]" (not const-qualified) cannot be initialized with a value of type "char *"

Any tips for how to get the compiler to accept value passed in the function BeforeDoingSomething?


As the error explains, you cannot initialize a reference to an array using a pointer.

If and only if you can prove that value does in fact point to (first element of) an array of appropriate type, then what you can do is explicitly reinterpret the pointer, and indirect it:



:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: