我有一个结构如下:
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返回并使用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)
;