I'm trying without success to copy a char array to another one. I have tried memcpy copying direcly the address from one to another, like this:
void include(int id, char name[16]) {
int i;
for (i = 0; i < SZ; i++) {
if (a[i].id == 0) {
a[i].id = id;
memcpy(&a[i].name, &name, strlen(name)+1);
return;
}
}
}
But obviously works only inside of this function. I have tried also like this: http://www.cplusplus.com/reference/clibrary/cstring/memcpy/ but it didn't work. Can someone help me?
a? You should change it to:memcpy(&a[i].name, name, strlen(name)+1);becausenameis an array, and an array's name is already a pointer to the first element.strcpyif the char arrays are null terminated?aas an argument to the functioninclude? Rather than having it as a global variable ...memcpyas it gives you the chance to protect against overrun in both buffers. Though ghe OP isn't exactly doing that...