Reputation: 4245
I have two arrays:
char roundNames1[16][25], roundNames2[16 / 2][25];
I then want to copy a result from the first array to the second. I have tried this:
where roundNames1[5] = "hello"
#include <string.h>
printf("First array: %s", roundNames1[5]);
strcpy(roundNames1[5], roundNames2[6]);
printf("Second array: %s", roundNames2[6]);
But this just returns
First array: hello
Second array:
Why is it not working?
Upvotes: 1
Views: 104
Reputation: 813
memcpy
void * memcpy (void * destination, void * source, size_t size)
The memcpy
function copies an certain amount of bytes from a source memory location and writes them to a destinaction location. (Documentation)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char some_array[] = "stackoverflow";
int memory_amount = sizeof(some_array);
char *pointer = malloc(memory_amount);
memcpy(pointer, &some_array, memory_amount);
printf("%s\n", pointer);
free(pointer);
return 0;
}
$ ./a.out
stackoverflow
Upvotes: 0
Reputation: 191
strcpy
- arguments other way around.
http://www.cplusplus.com/reference/cstring/strcpy/
Upvotes: 1
Reputation: 310930
You need to exchange arguments of function strcpy
strcpy( roundNames2[8], roundNames1[5] );
Here is a part pf the function description from the C Standard
7.23.2.3 The strcpy function
Synopsis
1
#include <string.h>
char *strcpy(char * restrict s1, const char * restrict s2);
Description
2 The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.
Upvotes: 2