0

I use a function

void * getCapabilities(...)

that returns other functions, depending on the given parameter.

Those functions can have different return types, hence, why I return void *. On of those functions is as follows:

void * getPeripherals(){
    UInt8 periphs[] = {21,22,23,24,25,26};
    return periphs;
}

I'd like to store this in a char array like so:

char cap_periph[7];
strcpy(cap_periph,  (char *)getPeripherals());
print("[CL] res_device_capabilities_get_handler. cap_periph[0] %u  cap_periph[5] %u \n", cap_periph[0],cap_periph[5]);

However, what is printed is bogus (85 and 0, respectively).

I am doing something wrong, but unsure of what. Can you help?

bash.d
  • 13,029
  • 3
  • 29
  • 42
  • 3
    `periphs` inside `getPeripherals` has automatic storage, local scope..... You cannot return its address. Otherwise is **Undefined Behavior** – LPs Dec 19 '16 at 13:22
  • Arrays aren't NULL byte terminated, so strcpy won't work on them. – Blue Granny Dec 19 '16 at 13:23
  • Void doesn't mean any. Very bad design, sorry. – vulkanino Dec 19 '16 at 13:23
  • 1
    @vulkanino void* means any return pointer type, I'm unsure as to why you would say it doesn't mean any? – Blue Granny Dec 19 '16 at 13:25
  • Technically, it is not returning a pointer to an object with automatic duration that causes UB; rather, it is afterward *using* that pointer as if to access the no-longer-existing object to which it initially pointed. – John Bollinger Dec 19 '16 at 14:01
  • Managed to fix it using your guys advice and the duplicate. Basically by defining periphs outside and returning that. Thanks for the help! –  Dec 19 '16 at 14:10

0 Answers0