tags:

views:

63

answers:

5

What do I need to change here so that animal contains {3,4}?

void funct(unsigned char *elf)
{
 unsigned char fish[2]={3,4};
 elf=fish;
}
int main()
{
 unsigned char animal[2]={1,2};
 funct(animal);
 return 0;
}

EDIT: I see memcpy is an option. Is there another way just manipulating pointers?

+1  A: 

One option is to assign the elements individually:

void funct(unsigned char *elf){
    elf[0] = 3;
    elf[1] = 4;
}

Another option is to use memcpy (which requires including string.h):

void funct(unsigned char *elf){
    unsigned char fish[2]={3,4};
    memcpy(elf, fish, 2);
}

memcpy takes as parameters the destination, the source, and then the number of bytes to copy, in this case 2.

Mark E
This basically memcpy(), right? Is there another way?
e2e8
+4  A: 

Since animal decays to a pointer when passed to a function, you can replace:

elf=fish;

with:

elf[0] = fish[0];        // or: *elf++ = fish[0]
elf[1] = fish[1];        //     *elf   = fish[1]

or, assuming they're the same size:

memcpy (elf, fish, sizeof (fish));

Post question edit:

EDIT: I see memcpy is an option. Is there another way just manipulating pointers?

There is no safe way to do this by manipulating the pointers if you mean changing the value of the pointer itself. If you pass in a pointer to a pointer, you can change it so that it points elsewhere but, if you point it at the fish array, you're going to get into trouble since that goes out of scope when you exit from funct().

You can use the pointer to transfer characters as per my first solution above (elf[n] array access is equivalent to *(elf+n) pointer access).

paxdiablo
`sizeof(fish)` because elf is passed as `char*` and not as `char[2]`!
ruslik
Ah, yes, silly me. Made the comment about decaying to a pointer, then totally ignored it :-) Fixed, thanks guys.
paxdiablo
+1  A: 
void funct(unsigned char *elf) {
    unsigned char fish[2]={3,4}; // stack variable, will dissapear after the function is exited
    // elf=fish; this one assigns to the local copy of elf, that will also dissapear when we return
    memcpy(elf, fish, sizeof(fish)); // so we have to copy values
    // careful though, because sizeof(fish) can be bigger than sizeof(elf)
}
ruslik
+4  A: 

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).

James McNellis
yeah, what he said!
David Gelhar
A: 
sskuce