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

5
  • 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. Commented May 20, 2022 at 14:37
  • 1
    "how to use gets()" - Simple: you don't; ever. Use fgets instead. First read how it works, then understand how it's different than gets, then adapt accordingly. Commented May 20, 2022 at 14:38
  • so what will we solution with fgets() Commented May 20, 2022 at 14:38
  • 1
    For starters - don't mix it with scanf. Commented May 20, 2022 at 14:39
  • Read the secret rules about using scanf successfully that no one ever teaches. Commented May 20, 2022 at 15:10

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for solution but their is any way we can use fgets()
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.
@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.
@Darth-CodeX: the space is necessary to consume the pending newline left by the previous call to scanf(). Answer amended with an appropriate explanation.
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;

2 Comments

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().
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.