c - 为什么 "code"与 "name"相同?

标签 c pointers

长颈鹿的名字和代码打印一次后由用户修改。我为每个输入不同的值,但“代码”与“名称”相同。

我相信我知道如何解决此错误,但我想知道如何解决此问题而不对我的代码进行太多修改或添加。我想这是我对指针的使用,因为这是我第一次接触它们。

看一下:

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 已更改。

不要使用名称代码的指针,而是使用足够大的数组来容纳任何预期的字符串,然后直接读入这些字符串。

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;
}

关于c - 为什么 "code"与 "name"相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56693265/

相关文章:

c - 在 C 中使用 fscanf 读取另一行

c - c中的文件处理操作

c - 内存依赖推测是否会阻止 BN_consttime_swap 成为恒定时间?

c - Little Endian 与 Big Endian 数据结构会改变我的答案吗?

c - 变量指针与内存指针

c - 如何在 C 函数中设置未初始化指针的值?

c - 在 C 中将二维数组作为双指针传递

带有 : : (double colon) syntax? 的 C 函数

c - 浮点乘法溢出

c - 在 C 中识别此 typedef 结构的各个部分?