Struct string array prints the last element entered

I want to print all entered elements. Instead, my code prints the last element typed 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 instance:

Enter

name:john

name:kendall

Output

kendall

kendall
+4
source share
4 answers

This is because you rewrite the value at each iteration.

You can create an array in main()and pass the array to functions so that the values ​​are stored in different places, instead you always pass the same instance of the structure gets()and, therefore, overwrite the previous value, so the print cycle prints the same data twice .

The following shows 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);
     }
 }

, , , , , , , , .

+3

. , :

  • ar->name. john, ar->name="john"
  • ar->name. , kendall, ar->name="kenadall".

john kendall, , kendall.

0

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

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

All Articles