c - 为什么“代码”与“名称”相同?
问题描述
长颈鹿的名字和代码打印一次后由用户修改。我为每个输入不同的值,但“代码”与“名称”相同。
我相信我知道如何解决这个错误,但我想知道如何在不对代码进行太多修改或添加的情况下解决这个问题。我认为这是我对指针的使用,因为这是我第一次接触它们。
看一看:
typedef struct {
int age;
double height;
char *name;
}giraffe;
void renameGiraffe(giraffe *g, char *na){
g->name = na;
}
void recodeGiraffe(char * codes[], int n, char * code){
codes[n] = code;
}
int main()
{
giraffe north; giraffe south; giraffe west;
north.name = "Fred";
south.name = "Bill";
west.name = "Kate";
char in1[] = "";
giraffe* exhibit[] = {&north, &south, &west};
char* codes[] = {"GN","GS","GW"};
for(int i = 0; i < (sizeof(exhibit)/sizeof(exhibit[0])); i++)
{
printf("The giraffe named %s has the code %s\n", exhibit[i]->name, codes[i]);
}
printf("Let's recode a giraffe. Which giraffe would you like to recode?\n\n");
scanf("%s", in1);
if(strcmp("north", in1)== 0)
{
printf("what is the new code for north?\n");
scanf("%s", in1);
recodeGiraffe(codes, 0, in1);
printf("North has been recoded. The new code for north is %s\n", in1);
}
printf("Let's rename the north giraffe. What's the new name?\n");
scanf("%s",in1);
renameGiraffe(&north, in1);
printf("Reprinting the list of giraffes now:\n\n");
for(int i = 0; i < (sizeof(exhibit)/sizeof(exhibit[0])); i++)
{
printf("The giraffe named %s has the code %s\n", exhibit[i]->name, codes[i]);
}
return 0;
}
我的输出:
The giraffe named Fred has the code GN // Ignore the other two giraffes.
The giraffe named Bill has the code GS
The giraffe named Kate has the code GW
Let's recode a giraffe. Which giraffe would you like to recode?
north
What is the new code for north?
NOR
North has been recoded. The new code for north is NOR
Let's rename the north giraffe. What's the new name?
FREDDY
Reprinting the list of giraffes now:
The giraffe named FREDDY has the code FREDDY //The code should be NOR.
The giraffe named Bill has the code GS
The giraffe named Kate has the code GW
解决方案
您正在读取新名称,in1
然后将name
作为指针的字段分配给指向in1
. 然后,您将代码读入n1
,这是该name
字段所指向的内容,因此name
已更改。
name
与其使用and的指针,不如使用codes
足够大的数组来保存任何预期的字符串,然后直接读入这些字符串。
typedef struct {
int age;
double height;
char name[100];
}giraffe;
int main()
{
giraffe north; giraffe south; giraffe west;
strcpy(north.name, "Fred");
strcpy(south.name, "Bill");
strcpy(west.name, "Kate");
char in1[100] = "";
giraffe* exhibit[] = {&north, &south, &west};
char codes[][100] = {"GN","GS","GW"};
for(int i = 0; i < (sizeof(exhibit)/sizeof(exhibit[0])); i++)
{
printf("The giraffe named %s has the code %s\n", exhibit[i]->name, codes[i]);
}
printf("Let's recode a giraffe. Which giraffe would you like to recode?\n\n");
scanf("%s", in1);
if(strcmp("north", in1)== 0)
{
printf("what is the new code for north?\n");
scanf("%s", codes[0]);
printf("North has been recoded. The new code for north is %s\n", codes[0]));
}
printf("Let's rename the north giraffe. What's the new name?\n");
scanf("%s",north.name);
printf("Reprinting the list of giraffes now:\n\n");
for(int i = 0; i < (sizeof(exhibit)/sizeof(exhibit[0])); i++)
{
printf("The giraffe named %s has the code %s\n", exhibit[i]->name, codes[i]);
}
return 0;
}
推荐阅读
- node.js - 没有调用 DeserializeUser 并且没有使用 express 和 passportJS 将 cookie 发送到客户端
- fasm - 为什么仅使用符号标志不能进行带符号的数字比较
- python - 使用 Opencv 和 Python 训练 SVM 和预测
- python - 是否可以通过 scikit.learn 创建一个预测分层类/标签的 RandomForestClassifier?
- arrays - 打字稿:如何在数组中按分钟分组?
- java - Spring MVC - 来自过滤器的内容类型感知响应
- javascript - Google Apps 脚本:在对象文档中找不到函数 insertText
- python - 管道 - 类型错误:fit_transform() 接受 2 个位置参数,但给出了 3 个
- ios - 在 Cordova iOS 包中找不到 `OBJROOT="$OBJROOT"` 以尝试修复构建错误:xcodebuild:命令失败,退出代码为 65
- mysql - 错误:ER_NO_DB_ERROR:始终使用数据库限定对象引用时未选择数据库(Node.js + MySql)