复制结构时出现段错误
Segfault on copying struct
我有一个结构如下:
extern struct team_t
{
char *name1;
char *email1;
char *name2;
char *email2;
} team;
struct team_t team =
{
"some string1",
"some string2",
"some string3",
"some string4"
};
然后在另一个文件中我创建了以下函数,将这个结构复制到一个新的结构中:
void *ucase( struct team_t *team)
{
struct team_t *ucase_team = malloc( sizeof *ucase_team);
memcpy ((char*)ucase_team, (char *)team, sizeof (ucase_team));
return NULL;
}
但是,当我想调用 ucase(team)
时,出现了段错误。我需要使用 void *
因为这稍后将用于 shell 信号。我错过了什么?
更新:后续调用给出 type argument of unary ‘*’ (have ‘struct team_t’)
错误:
ucase(*team)
更新 2:我删除了 Null return 并使用了 ucase(team)
但仍然出现段错误。
memcpy()
的最后一个参数应该是 sizeof(struct team_t)
而不是 sizeof (ucase_team)
因为 ucase_team
是一个结构指针变量。它可以是 sizeof(*ucase_team)
或 sizeof(struct team_t)
.
也像
一样调用team()
函数
ucase(*team);
是错误的,因为 team
不是指针类型的变量,它是一个普通的结构变量。可能你想要
ucase(&team)
;
我有一个结构如下:
extern struct team_t
{
char *name1;
char *email1;
char *name2;
char *email2;
} team;
struct team_t team =
{
"some string1",
"some string2",
"some string3",
"some string4"
};
然后在另一个文件中我创建了以下函数,将这个结构复制到一个新的结构中:
void *ucase( struct team_t *team)
{
struct team_t *ucase_team = malloc( sizeof *ucase_team);
memcpy ((char*)ucase_team, (char *)team, sizeof (ucase_team));
return NULL;
}
但是,当我想调用 ucase(team)
时,出现了段错误。我需要使用 void *
因为这稍后将用于 shell 信号。我错过了什么?
更新:后续调用给出 type argument of unary ‘*’ (have ‘struct team_t’)
错误:
ucase(*team)
更新 2:我删除了 Null return 并使用了 ucase(team)
但仍然出现段错误。
memcpy()
的最后一个参数应该是 sizeof(struct team_t)
而不是 sizeof (ucase_team)
因为 ucase_team
是一个结构指针变量。它可以是 sizeof(*ucase_team)
或 sizeof(struct team_t)
.
也像
一样调用team()
函数
ucase(*team);
是错误的,因为 team
不是指针类型的变量,它是一个普通的结构变量。可能你想要
ucase(&team)
;