Difference in memcpy and strncpy for structured copy
I have the code below. I am trying to copy a structure to a string. I want to understand why the output depends on strncpy and memcpy.
#include <stdio.h>
#include<string.h>
struct a{
int len;
int type;
};
int main(){
struct a aa={98,88};
char str[10]="";
char str2[10]="";
strncpy(str,&aa,sizeof(struct a));
memcpy(str2,&aa,sizeof(struct a));
for(int i=0;i<10;i++)printf("%2d",str[i]);
printf("\n");
for(int i=0;i<10;i++)printf("%2d",str2[i]);
return 0;
}
The output is shown below:
98 0 0 0 0 0 0 0 0 0
98 0 0 088 0 0 0 0 0
I understand that strncpy will copy until it hits '\ 0' (or size limit), but I don't have a '\ 0' value in the structure. Can someone please help me understand this. Purpose of this: trying to send the structure over the network. Although I am planning to implement serialization, I would like to understand the behavior
EDIT: 1) Suggested by Keith Thompson
Below is a warning.
incompatible pointer types passing 'struct a *' to parameter of type 'const char *' [-Wincompatible-pointer-types]
2) I slightly modified the code to use an int array:
(Post this for reference. I understand that in this case memcpy is copying the struct variable in the first two elements of the array, since the size is large enough for the structure variable.)
#include <stdio.h>
#include<string.h>
struct a{
int len;
int type;
};
int main(){
struct a aa={98,88};
int str[10]={0};
int str2[10]={0};
strncpy(str,&aa,sizeof(struct a));
memcpy(str2,&aa,sizeof(struct a));
for(int i=0;i<10;i++)printf("%2d",str[i]);
printf("\n");
for(int i=0;i<10;i++)printf("%2d",str2[i]);
return 0;
}
Below is o \ p:
98 0 0 0 0 0 0 0 0 0
9888 0 0 0 0 0 0 0 0
Below are the warnings:
incompatible pointer types passing 'int [10]' to parameter of type 'char *' [-Wincompatible-pointer-types]
incompatible pointer types passing 'struct a *' to parameter of type 'const char *' [-Wincompatible-pointer-types]
source to share
but I don't have a "\ 0" value in the structure.
You actually have at least six '\0'
-s: assuming int
is 32-bit, the top three bytes of both 98
and 88
are all zeros. They will make strncpy
a copy stop. This function is for fixed length strings, so it cannot be used with arbitrary struct
s. memcpy
, on the other hand, will copy everything.
Purpose of this: trying to send the structure over the network
If you want to send yours struct
over the network and you want the packet to be portable, convert both int
to network order on the sender side and revert to the equipment order on the receiving side. For 32-bit numbers, use htonl
and ntohl
function .
source to share
but I don't have a "\ 0" value in the structure.
Yes Yes. Integer values have 0 bits, which can be interpreted as '\0'
when byte data is interpreted as characters. Since it strncpy
works "character by character until it reaches the terminator", it makes it stop earlier.
memcpy
copies the specified number of bytes, always, which makes it work. In this case, it is more appropriate.
source to share