为什么“代码”与“名称”相同?

时间:2019-06-20 20:13:10

标签: 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

1 个答案:

答案 0 :(得分:4)

您正在将新名称读入in1,然后分配name字段(它是一个指针)指向in1。然后,您将代码读入n1所指向的name中,因此name已更改。

不要使用namecodes的指针,而是使用足够大的数组来容纳任何期望的字符串,然后直接读取这些字符串。

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