函数如何创建结构体? [英] How can a function create a struct?
问题描述
我在 C 编程课程中的一项作业是定义一个名为 create_card 的函数.该函数接收一个花色字符和一个卡片值,并返回一个 card_t 结构体.
One of my assignments for the C Programming course was define a function called create_card. This function receives a suit character and an a card value, and returns a card_t struct.
问题:函数应该如何创建结构体?它不能只为结构创建值吗?是我误解了问题的意思还是作业写错了?
Question: How is a function supposed to create a struct? Can't it only create values for the struct? Did I misinterpret the meaning of the question or was the assignment written down wrong?
推荐答案
这是一个返回结构体的函数示例.
This is an example of a function returning a struct.
struct test {
int a;
};
struct test Fun(int k) {
struct test d;
d.a=k;
return d;
}
将 struct test
替换为您的结构名称,并将 struct test
的定义替换为您的结构定义.
Replace struct test
with the name of your struct and the definition of struct test
with your struct's definition.
如何使用
int main() {
struct test Test=Fun(6);
printf("%d",Test.a); // prints '6'
return 0;
}
这篇关于函数如何创建结构体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!