gpt4 book ai didi

c - 在结构中添加字符

转载 作者:太空宇宙 更新时间:2023-11-04 03:41:31 25 4
gpt4 key购买 nike

<分区>

为什么我不能将 name 添加到 data 函数中?

如果我运行这段代码

data->userName = name;

那一行是错误的。为什么我不能将 userName 设置为 name

#import <Foundation/Foundation.h>
typedef struct User {
char userName[5];
int userAge;
} User;
int roomNum = 0;
void data (User* data, int age, char* name);

main () {
int age;
char name[5];
User list[10] = {};
for (int i = 0; i<3; i++) {
scanf("%d",&age);
scanf("%s",name);

data(&list[roomNum],age,name);
roomNum ++;
}

NSLog(@"%d %s",list[0].userAge,list [0].userName);
NSLog(@"%d %s",list[1].userAge,list[1].userName);
NSLog(@"%d %s",list[2].userAge,list[2].userName);
}

void data (User* data, int age, char* name){
NSLog(@"%s",name);
(*data).userAge = age;
data->userName = name;
}

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