c语言结构体多次调用错误,一个关于c语言结构体及其调用的问题

在学习C语言时遇到结构体使用问题,代码中定义的结构体成员在函数调用时出现未定义的错误。通过修改函数参数引用方式,将成员前加上结构体变量名前缀,如`f.name`,解决了编译错误。示例代码展示了正确的结构体成员访问方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在学习c语言,有好多问题都不懂啊。然后在结构体又出问题了

104dc975c9504a40705f34fa2f5549c2.gif

在下面的代码中,编译过程中总是提示name, species, teeth, age这四个标识符未定义,但是在前面的结构体中不是已经定义了吗。这个该怎么办啊,小的实在想不出来啦。

struct fish {

const char *name;

const char *species;

int teeth;

int age;

} ;

void catalog(struct fish f)

{

printf(“%s is a %s with %i teeth. He is %i.\n”, name, species, teeth, age);

}

void label(struct fish f)

{

printf(“Name: %s\nSpecies: %s\n%i years old,\n%i teeth\n”, name, species, teeth, age);

}

struct fish {

const char *name;

const char *species;

int teeth;

int age;

} ;

int main()

{

struct fish snappy = {“Snappy”, “Piraha”, 69, 4};

catalog(snappy);

label(snappy);

return 0;

}

解决方案

20

void catalog(struct fish f)

{

printf(“%s is a %s with %i teeth. He is %i.\n”, f,name, f.species, f.teeth, f.age);

}

其余的相似

40

#include

struct fish {

const char *name;

const char *species;

int teeth;

int age;

} ;

void catalog(struct fish f)

{

printf("%s is a %s with %i teeth. He is %i.\n", f.name, f.species, f.teeth, f.age);

}

void label(struct fish f)

{

printf("Name: %s\nSpecies: %s\n%i years old,\n%i teeth\n", f.name, f.species, f.teeth, f.age);

}

int main()

{

struct fish snappy = {"Snappy", "Piraha", 69, 4};

catalog(snappy);

label(snappy);

return 0;

}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明一个关于c语言结构体及其调用的问题!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值