#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char d2arr[6][7] = {
{ 'a', 'a', 'a', 'a', 'a', 'a' },
{ 'b', 'b', 'b', 'b', 'b', 'b' },
{ 'c', 'c', 'c', 'c', 'c', 'c' },
{ 'd', 'd', 'd', 'd', 'd', 'd' },
{ 'e', 'e', 'e', 'e', 'e', 'e' },
};
char d3arr[6][7][7];
memset(d3arr, NULL, sizeof(d3arr));
strncpy(d2arr[0], d3arr[0][0], sizeof(7));
strncpy(d2arr[1], d3arr[0][1], sizeof(7));
strncpy(d2arr[2], d3arr[0][2], sizeof(7));
strncpy(d2arr[3], d3arr[0][3], sizeof(7));
strncpy(d2arr[4], d3arr[0][4], sizeof(7));
printf("%s\n", d3arr[0][0]);
printf("%s\n", d3arr[0][1]);
printf("%s\n", d3arr[0][2]);
printf("%s\n", d3arr[0][3]);
printf("%s\n", d3arr[0][4]);
return 0;
}
There is no error message, and the results do not come out, so I ask questions. Please let me know why the string in the 2D array is not copied.
CodePudding user response:
Your code does not behave as expected:
- the first argument to
strncpy
is the destination pointer. - the second argument is the source pointer
- the third argument is the size in bytes at the destination to stop the copy or up to which to pad with null bytes if the source string is shorter. You pass
sizeof(7)
which is most likely4
on your system, whereas you should just pass7
.
Note however that strncpy
is a very poorly understood function that does not behave as most programmers expect and is error prone for everyone. Here is sound advice about this function: Stop using strncpy
already!
In your case, calling strcpy
or even memcpy
works fine.
Also do not use NULL
for a null byte as the second argument to memcpy
: use '\0'
or 0
.
#include <stdio.h>
#include <string.h>
int main(void) {
char d2arr[][7] = {
{ 'a', 'a', 'a', 'a', 'a', 'a' },
{ 'b', 'b', 'b', 'b', 'b', 'b' },
{ 'c', 'c', 'c', 'c', 'c', 'c' },
{ 'd', 'd', 'd', 'd', 'd', 'd' },
{ 'e', 'e', 'e', 'e', 'e', 'e' },
};
char d3arr[6][7][7];
memset(d3arr, '\0', sizeof(d3arr));
strcpy(d3arr[0][0], d2arr[0]);
strcpy(d3arr[0][1], d2arr[1]);
strcpy(d3arr[0][2], d2arr[2]);
strcpy(d3arr[0][3], d2arr[3]);
strcpy(d3arr[0][4], d2arr[4]);
printf("%s\n", d3arr[0][0]);
printf("%s\n", d3arr[0][1]);
printf("%s\n", d3arr[0][2]);
printf("%s\n", d3arr[0][3]);
printf("%s\n", d3arr[0][4]);
memcpy(d3arr[1][0], d2arr[0], 7);
memcpy(d3arr[1][1], d2arr[1], 7);
memcpy(d3arr[1][2], d2arr[2], 7);
memcpy(d3arr[1][3], d2arr[3], 7);
memcpy(d3arr[1][4], d2arr[4], 7);
printf("%s\n", d3arr[1][0]);
printf("%s\n", d3arr[1][1]);
printf("%s\n", d3arr[1][2]);
printf("%s\n", d3arr[1][3]);
printf("%s\n", d3arr[1][4]);
return 0;
}
CodePudding user response:
The problem is here:
strncpy(d2arr[0] , d3arr[0][0] , sizeof(7) );
strncpy(d2arr[1] , d3arr[0][1] , sizeof(7) );
strncpy(d2arr[2] , d3arr[0][2] , sizeof(7) );
strncpy(d2arr[3] , d3arr[0][3] , sizeof(7) );
strncpy(d2arr[4] , d3arr[0][4] , sizeof(7) );
The strncpy function works as under:
strncpy(destination, source, no_of_characters);
But in your code you have swapped destination and source arrays, and as a result, your code doesn't work. Also instead of using 'sizeof(7)' you should use only '7'. Also you should use strncpy_s instead of strncpy as strncpy can be unsafe.
So after all the corrections, here's the code:
strncpy_s(d3arr[0][0], d2arr[0], 7);
strncpy_s(d3arr[0][1], d2arr[1], 7);
strncpy_s(d3arr[0][2], d2arr[2], 7);
strncpy_s(d3arr[0][3], d2arr[3], 7);
strncpy_s(d3arr[0][4], d2arr[4], 7);
Just replace the part with strncpy functions with the above code, and you'll be good to go. Other part of the code is totally fine.