Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

strncpy 的怪事

0 views
Skip to first unread message

網路連線中.......

unread,
Nov 11, 1996, 3:00:00 AM11/11/96
to

R_RSA_PUBLIC_KEY publickey;
strncpy((char *)&publickey, (char *)PointToKey,
sizeof(R_RSA_PUBLIC_KEY));

以上 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 *)&publickey, (char *)PointToKey,
sizeof(R_RSA_PUBLIC_KEY));

自己寫一個 strncpy_my 結果就沒問題了,是什麼原因呢???

執著

unread,
Nov 11, 1996, 3:00:00 AM11/11/96
to

==> 在 Bigto...@vlsi1.iie.ncku (網路連線中.......) 的文章中提到:

> R_RSA_PUBLIC_KEY publickey;
> strncpy((char *)&publickey, (char *)PointToKey,
^^^^^^^^^^^^^^^^^^

> sizeof(R_RSA_PUBLIC_KEY));
> 以上 string copy 時會有問題
> R_RSA_PUBLIC_KEY 是一個 structure
> unsigned char *PointToKey;
> 我想把一個 structure 的 date 都 copy 到 PointToKey 這個 pointer
^^^^^^^^^^^^^^^^^^

> 所指的位址,結果資料 copy 有問題,並非我想要的。

char *strncpy(char *dest, const char *src, size_t maxlen);
^^^^^^^^^^^^^^^
你 src, dest 的位置搞錯了吧!

--
* Origin: ★ 交通大學資訊科學系 BBS ★ <bbs.cis.nctu.edu.tw: 140.113.23.3>

0 new messages