【问题标题】:Struct string array print last entered element结构字符串数组打印最后输入的元素
【发布时间】:2015-05-25 14:55:42
【问题描述】:

我想打印所有输入的元素。相反,我的代码将最近输入的元素打印两次。

这是我的代码:

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

例如:

输入

name:john

name:kendall

输出

kendall

kendall

【问题讨论】:

  • 您永远不能使用gets(),而是使用fgets()。这就是 gcc 所说的 警告:`gets' 函数很危险,不应使用。
  • 你需要创建一个链表。 Linked data Structure
  • 所以在你的循环中你得到/打印a-&gt;name 两次而不移动它。那么你期望得到什么?
  • @chengpohi 我不这么认为。

标签: c arrays struct


【解决方案1】:

这是因为您在每次迭代中都覆盖了该值。

您可以在main() 中创建一个数组并将该数组传递给函数,以便将值存储在不同的位置,而不是将相同的结构实例传递给gets() 并因此覆盖以前的值,因此打印循环打印相同的数据两次。

下面演示如何传递一个数组

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

另外,在命名标识符时不要偷懒,即使是简单的演示,它们也有助于使程序意图清晰,并帮助您永远维护它,如果您认为这无关紧要,那么恐怕当你做一个真实的项目时,你会遇到很多问题。

【讨论】:

  • 感谢您的回答,我会考虑您的建议
【解决方案2】:

在这里,您在同一个变量中输入两次。那么发生了什么:

  • 在第一次迭代中,输入存储在ar-&gt;name 中。第一个输入是john,所以ar-&gt;name="john"
  • 在下一次迭代中,输入也将存储在ar-&gt;name 中。所以如果你的输入是kendall,那么ar-&gt;name="kenadall"

因此,john 值将被kendall 覆盖,并且在打印时您将获得kendall 值。

【讨论】:

    【解决方案3】:

    有一个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;
        }
    }
    

    【讨论】:

      【解决方案4】:

      排队

      gets(a->name);
      

      每次 for 循环运行时,您都将用户输入记录到同一位置。相反,尝试类似

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

      【讨论】:

        猜你喜欢
        • 2021-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-29
        • 1970-01-01
        • 2013-02-16
        • 2019-07-19
        • 2017-06-12
        相关资源
        最近更新 更多