gpt4 book ai didi

c - 如何将 struct 和 int 从函数传递回 main? C

转载 作者:行者123 更新时间:2023-11-30 18:50:47 26 4
gpt4 key购买 nike

我试图将数组 person 和数量_persons_count 返回到 main() 但我无法让它工作。我尝试将 void 更改为 int 和 person,但该 obv 不起作用。

struct person{..}
int main(){
int o;
int quantity_persons_count = 0;
struct person persons[100];

while(1){

printf("1.Add a new person");
scanf("%i",&o);

switch(o)
{
case 1: AddPerson(persons,quantity_persons_count);
break;
}

void AddPerson(struct person *persons, int quantity_persons_count){
if(quantity_persons_count == 100){
printf("ERROR.\n");
}
else{
printf("name\n");
scanf("%s",persons[quantity_persons_count+1].name);
quantity_persons_count++;
printf("done\n");

}

}

最佳答案

如果您希望对 quantity_persons_count 的更改在 main 中可见,则需要向其传递一个指针:

void AddPerson(struct person *persons, int *quantity_persons_count){

if(*quantity_persons_count == 100){
printf("ERROR.\n");
}
else{
printf("name\n");
scanf("%s",persons[*quantity_persons_count+1].name);
(*quantity_persons_count)++;
printf("done\n");

}

}

然后你这样调用它:

AddPerson(persons,&quantity_persons_count);

关于c - 如何将 struct 和 int 从函数传递回 main? C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39049673/

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