以上 string copy 時會有問題
R_RSA_PUBLIC_KEY 是一個 structure
unsigned char *PointToKey;
我想把一個 structure 的 date 都 copy 到 PointToKey 這個 pointer
所指的位址,結果資料 copy 有問題,並非我想要的。
我就改成下面的樣子
void strncpy_my(char *st1, char *st2, int n)
{
int i;
for (i=0;i<n;i++)
*(st1+i) = *(st2+i);
}
R_RSA_PUBLIC_KEY publickey;
strncpy_my( (char *)PointToKey,(char *)&publickey,
sizeof(R_RSA_PUBLIC_KEY));
自己寫一個 strncpy_my 結果就沒問題了,是什麼原因呢???
your strncpy_my is not a real "strncpy" for 2 reasons:
1. u do not check the end mark of a string, that is '\0'.
2. u only copy n bytes from st2 to st1, that is, u r copying memory, NOT
copying string.
So i think that what u want is not strncpy: u should use memcpy(...),
movmem(...), memmov(...) depending on ur complier and OS.
thank you..... ^_^
沒想到用 memmov(),我知道了。
strncpy(&PointToKey,&publickey,sizeof(R_RSA_PUBLIC_KEY);