简体   繁体   English

结构字符串数组打印最后输入的元素

[英]Struct string array print last entered element

I want to print all entered elements. 我想打印所有输入的元素。 Instead, my code prints the most recently entered element twice. 相反,我的代码打印最近输入的元素两次。

Here is my code: 这是我的代码:

#include<stdio.h>
void f(struct ar *a);
void d(struct ar *a);
struct ar
{
    char name[50];
};
int main()
{
    struct ar a;
    f(&a);
    d(&a);
}
void f(struct ar *a)
{
    int i;
    for(i=0;i<2;i++)
    {
        printf("enter name:");
        gets(a->name);
    }
}
void d(struct ar *a)
{
    int i;
    for(i=0;i<2;i++)
    {
        puts(a->name);
    }
}

For example: 例如:

Input 输入

name:john

name:kendall

Output 产量

kendall

kendall

It's because you are overwriting the value in each iteration. 这是因为你在每次迭代中都覆盖了这个值。

You can create an array in main() and pass the array to the functions so the values are stored in different places, instead you always pass the same structure instance to gets() and hence overwrite the previous value, so the printing loop prints the same data twice. 您可以在main()创建一个数组并将数组传递给函数,以便将值存储在不同的位置,而不是总是将相同的结构实例传递给gets() ,从而覆盖以前的值,因此打印循环将打印出来相同的数据两次。

The following demonstrate how to pass an array 以下演示如何传递数组

#include <stdio.h>

struct Data
 {
    char name[50];
 };

void readData(struct Data *array);
void showData(struct Data *array);

int main()
 {
    struct Data array[2];

    readData(array);
    showData(array);
 }

void readData(struct Data *array)
 {
    int i;
    for (i = 0 ; i < 2 ; i++)
     {
        printf("enter name: ");
        fgets(array[i].name, sizeof(array[i].name), stdin);
     }
 }

void showData(struct Data *array)
 {
    int i;
    for (i = 0 ; i < 2 ; i++)
     {
        printf("%s", array[i].name);
     }
 }

Also, don't be lazy when it comes to naming identifiers, even for a simple demonstration they help make the program intent clear and they help you maintain it forever, if you think that doesn't matter, then I am afraid you will have a lot of problems when you work on a real life project. 此外,在命名标识符时不要太懒,即使是简单的演示,它们也有助于使程序意图清晰,并且它们可以帮助您永远保持它,如果您认为无关紧要,那么我担心您会有当你从事现实生活中的项目时遇到很多问题。

Here you are taking the inputs twice in same variable. 在这里,您将在同一变量中输入两次输入。 So what the happening is: 那么发生了什么:

  • In the first iteration the input is getting stored in ar->name . 在第一次迭代中,输入存储在ar->name The first input is john , so ar->name="john" 第一个输入是john ,所以ar->name="john"
  • In the next iteration the input is also getting stored in ar->name . 在下一次迭代中,输入也存储在ar->name So if your input is kendall then ar->name="kenadall" . 因此,如果您的输入是kendall那么ar->name="kenadall"

For this reason the john value is getting overwritten by kendall and when you are printing you are getting the kendall value. 因此, kendall覆盖john值,当您打印时,您将获得kendall值。

There is a Linked List to do the same thing: 有一个Linked List可以做同样的事情:

#include <stdio.h>
#include <stdlib.h>


struct Data
{
    char name[50];
    struct Data *next;
};

struct Data *readData();
void showData(struct Data *array);

int main()
{
    struct Data *array;

    array = readData();
    showData(array);
}

struct Data *readData()
{
    int i;
    struct Data *tmp = NULL;
    struct Data *head = NULL;
    struct Data *end = head;

    for (i = 0 ; i < 2 ; i++)
    {
        tmp = (struct Data *)malloc(sizeof(struct Data));
        printf("enter name: ");
        fgets(tmp->name, sizeof(tmp->name), stdin);
        if (head == NULL) {
            head = tmp;
        } else {
            end->next = tmp;
        }
        end = tmp;
    }
    return head;
}

void showData(struct Data *array)
{
    int i;
    struct Data *a = array;
    while(a != NULL) {
        printf("%s\n", a->name);
        a = a->next;
    }
}

In the line 在线

gets(a->name);

you are recording the user input to same place each time the for loop runs. 每次for循环运行时,您都将用户输入记录到同一位置。 Instead, try something like 相反,尝试类似的东西

#include<stdio.h>

struct Name
{
  char first[50];
  char last[50];
};

void recordName(struct Name *name); //writes Name structs
void reportName(struct Name *name); //prints contents of Name structs


int main()
{
  struct Name test_Name;

  recordName(&test_Name);
  reportName(&test_Name);

  return 0;
}

void recordName(struct Name *name)
{
  printf("enter first Name: ");
  fgets(name->first,sizeof(name->first),stdin);

  printf("enter last Name: ");
  fgets(name->last,sizeof(name->last),stdin);
}


void reportName(struct Name *name)
{
  printf("%s%s",name->first, name->last);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM