I need to take a char [] array and copy it’s value to another, but I fail every time.
It works using this format:
char array[] = { 0x00, 0x00, 0x00 }
However, when I try to do this:
char array[] = char new_array[];
it fails, even though the new_array is just like the original.
Any help would be kindly appreciated.
Thanks
To copy at runtime, the usual C method is to use the
strncpyormemcpyfunctions.If you want two
chararrays initialized to the same constant initializer at compile time, you’re probably stuck with using#define:Thing is, this is rarely done because there’s usually a better implementation.
EDIT: Okay, so you want to copy arrays at runtime. This is done with
memcpy, part of<string.h>(of all places).If I’m reading you right, you have initial conditions like so:
Then you do something, changing the arrays’ contents, and after it’s done, you want to set
arrayto matchnew_array. That’s just this:The library writers chose to order the arguments with the destination first because that’s the same order as in assignment:
destination = source.There is no language-level built-in means to copy arrays in C, Objective-C, or C++ with primitive arrays like this. C++ encourages people to use
std::vector, and Objective-C encourages the use ofNSArray.I’m still not sure of exactly what you want, though.