struct book1{

char word[7];

char word2[7];

}

void test(struct book1 *aa)

{

char buf[15],buf2[15];

//这样没有问题

strcpy(aa->word,"aaaaaaa");

memcpy(buf,aa->word,6);

//这样做就完之后aa->word,buf就全为空了。

strcpy(buf2,aaaaaaa");

strcpy(aa->word,buf2);

memcpy(buf,aa->word,6);

}

main()

{

struct book1 *bb;

bb=(struct book1*)malloc(sizeof(struct book1));

test(bb);

}

以上内容只是示意,buf2是从别的子函数传来的结构体也还有很多内容。没有加入。期待高手解决

|

从你这个示意来看,应该没有你所说的问题。

估计是在其他什么地方出了问题

- 千里孤行

|

怎么可能?

$ cat love.c && make love && ./love

#include

#include

#include

struct book1 {

char            word[7];

char            word2[7];

};

void

test(struct book1 *aa)

{

char            buf[15], buf2[15];

/* 这样没有问题 */

memset(buf, 0, sizeof(buf));

strcpy(aa->word, "aaaaaa");

memcpy(buf, aa->word, 6);

/* 这样做就完之后aa->word,buf就全为空了。 */

memset(buf, 0, sizeof(buf));

strcpy(buf2, "aaaaaa");

strcpy(aa->word, buf2);

printf("%sn", aa->word);

memcpy(buf, aa->word, 6);

printf("%sn", aa->word);

}

int

main()

{

struct book1   *bb;

bb = (struct book1 *)malloc(sizeof(struct book1));

test(bb);

}

cc -O2 -pipe   love.c  -o love

aaaaaa

aaaaaa

|

请注意malloc 与 free 配对使用!!!!

|

设置个断点gdb跟踪一下吧。

Logo

一站式 AI 云服务平台

更多推荐