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 12, 1996, 3:00:00 AM11/12/96
to

==> Bigtown@bar (網路連線中.......) 在 oop 版提到:
: 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 結果就沒問題了,是什麼原因呢???
SORRY 筆誤,以下才正確
R_RSA_PUBLIC_KEY publickey;
strncpy((char *)PointToKey,(char *)&publickey,
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 *)PointToKey,(char *)&publickey,
sizeof(R_RSA_PUBLIC_KEY));

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

cjh

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

==> Bigtown@bar (網路連線中.......) 在 oop 版提到:
: ==> Bigtown@bar (網路連線中.......) 在 oop 版提到:

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.

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

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

==> chenjh@bar (cjh) 在 oop 版提到:

: ==> Bigtown@bar (網路連線中.......) 在 oop 版提到:
: : SORRY 筆誤,以下才正確

: : R_RSA_PUBLIC_KEY publickey;
: : strncpy((char *)PointToKey,(char *)&publickey,
: : 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 *)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(),我知道了。

c...@tpts1.seed.net.tw

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

In <3FXflJ$4...@vlsi1.iie.ncku.edu.tw>, Bigto...@vlsi1.iie.ncku.edu.tw (網路連線中.......) writes:
>==> Bigtown@bar (網路連線中.......) 在 oop 版提到:
>SORRY 筆誤,以下才正確
> R_RSA_PUBLIC_KEY publickey;
> strncpy((char *)PointToKey,(char *)&publickey,
> 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 *)PointToKey,(char *)&publickey,
> sizeof(R_RSA_PUBLIC_KEY));
>
> 自己寫一個 strncpy_my 結果就沒問題了,是什麼原因呢???

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

0 new messages