gpt4 book ai didi

c - 使用 C 在其他结构的结构中存储值时出现段错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:16:23 27 4
gpt4 key购买 nike

我有两个结构

typedef struct profile_datagram_t
{
unsigned char *src;
unsigned char *dst;
unsigned char ver;
unsigned char n;
struct profile_t profiles[MAXPROFILES];
} header;

header outObj;

struct pearson_record
{
unsigned char *src;
};

现在我想将 outObj.src 中的值 memcpy 到 struct pearson_record 的 unsigned char *src 中。

如何做到这一点?任何类型的例子或任何帮助都会有很大帮助。提前致谢。

最佳答案

假设 outObj.src 指向包含以 null 结尾的字符串的有效内存:

struct person_record x;
x.src = malloc(strlen(outObj.src) + 1); // +1 for the null terminator!
strcpy(x.src, outObj.src); // could have used strncpy as well

关于c - 使用 C 在其他结构的结构中存储值时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7184498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com