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

struct data
{
    char name[100];
    int age;
};

int main()
{
    struct data *p;
    int i, n;
    printf("\nENTER THE SIZE OF STRUCTURE:");
    scanf("%d", &n);
    p = (struct data *)calloc(n, sizeof(struct data));

    if (n < 0 || p == NULL)
    {
        printf("\nSTUCTURE DOESNOT CREATED");
    }
    else
    {
        for (i = 0; i < n; i++)
        {
            printf("\nENTER THE INFO FOR %d STRUCTER", i + 1);
            printf("\nENTER THE NAME:");
            gets((p + i)->name);

            printf("\nENTER THE AGE:");
            scanf("%d", &(p + i)->age);
        }
        for (i = 0; i < n; i++)
        {
            printf("\n");
            printf("\n%d\t\t%s\t\t%d", i + 1, (p + i)->name, (p + i)->age);
        }
        free(p);
    }
}

can I use gets() function to store string in this above code I know I can store with scanf() but it will terminated after white spaces and the reference of this program https://www.programiz.com/c-programming/examples/structure-dynamic-memory-allocation

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    Do not use `gets` at all. This function is deprecated for use with older C standard revisions and is completely removed from the newer ones. You can use `fgets` though. – Eugene Sh. May 20 '22 at 14:37
  • 1
    *"how to use gets()"* - Simple: [you *don't*](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used); *ever*. Use [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. First read how it works, then understand how it's different than `gets`, then adapt accordingly. – WhozCraig May 20 '22 at 14:38
  • so what will we solution with fgets() – Ayush Talesara May 20 '22 at 14:38
  • 1
    For starters - don't mix it with `scanf`. – Eugene Sh. May 20 '22 at 14:39
  • Read the [secret rules about using `scanf` successfully](https://stackoverflow.com/questions/72178518/how-can-i-fix-the-scanf-to-take-data-into-the-array/72178652#72178652) that no one ever teaches. – Steve Summit May 20 '22 at 15:10

2 Answers2

2

how to use gets() function?

Do not use gets() for anything. This function is obsolete and cannot be used safely. Instead of gets((p + i)->name), you can write:

scanf(" %99[^\n]", p[i].name);

Note the initial space in the scanf format string: it tells scanf() to skip initial white space, which is necessary to consume the pending newline into the name field. Without this space, scanf("%99[^\n]", p[i].name); would fail and return 0 because no characters from stdin would match the conversion specification before the newline.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • thanks for solution but their is any way we can use fgets() – Ayush Talesara May 20 '22 at 14:46
  • Mixing `fgets()` and `scanf()` is tricky because `scanf()` typically leaves the newline pending in `stdin` and `fgets()` will read an empty line if called immediately after. – chqrlie May 20 '22 at 14:49
  • @AyushTalesara It's barely possible to mix `fgets` and `scanf` in the same program, but it's at least twice as hard and twice as confusing as just using `fgets` everywhere, or using `scanf` everywhere. – Steve Summit May 20 '22 at 15:07
  • @Darth-CodeX: the space is necessary to consume the pending newline left by the previous call to `scanf()`. Answer amended with an appropriate explanation. – chqrlie May 21 '22 at 08:03
1

Never use gets() function it is obsolete.

Try using fgets()

fgets(p[i].name, sizeof(p[i].name), stdin);
p[i].name[strcspn(p[i].name, "\n")] = 0;
Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23
  • Use `sizeof(p[i].name)` instead of `99`, which will also avoid the off-by-one error. You can safely specify the exact size of the array to `fgets()`. – Jonathan Leffler May 20 '22 at 20:47
  • This does not work because `scanf("%d",...)` left a pending newline in `stdin` that will be read by `fgets()` as an empty line. Mixing `scanf()` and `fgets()` is tricky indeed. – chqrlie May 21 '22 at 08:05