Is there another way just manipulating pointers?
No, because animal
is not a pointer. animal
is an array. When you pass it as an argument to the function, it decays to a pointer to its first element, just as if you had said &animal[0]
.
Even if you use a pointer and take a pointer to it in funct
, it still won't work:
void funct(unsigned char** elf)
{
unsigned char fish[2] = { 3, 4 };
*elf = fish; // oh no!
}
int main()
{
unsigned char animal[2] = { 1, 2 };
unsigned char* animal_ptr = animal;
funct(&animal_ptr);
}
After the line marked "oh no!" the fish
array ceases to exist; it goes away when funct
returns because it is a local variable. You would have to make it static or allocate it dynamically on order for it to still exist after the function returns.
Even so, it's still not the same as what you want because it doesn't ever modify animal
; it only modifies where animal_ptr
points to.
If you have two arrays and you want to copy the contents of one array into the other, you need to use memcpy
(or roll your own memcpy
-like function that copies array elements in a loop or in sequence or however).