-1

i want to create a program where user will give info as input to form their NID CARD.my program is ending just after taking the first input.i have also used %[^\n] instead of %s, but the same things happen.

#include <stdio.h>

struct DOB
{
        int date;
        char* month;
        int year;
};

struct NID
{
        char* name_bangla;
        char* name_eng;
        char* f_name;
        char* m_name;
        struct DOB s;
        int ID;
};


int main()
{
    struct NID my;
    scanf(" %s", my.name_bangla);
    fflush(stdin);
    scanf(" %s", my.name_eng);
    fflush(stdin);
    scanf(" %s", my.f_name);
    fflush(stdin);
    scanf(" %s", my.m_name);
    scanf("%d", &my.s.date);
    scanf("%[^\n]", my.s.month);
    scanf("%d", &my.s.year);
    scanf("%d", &my.ID);

    printf("name b :  %s\n", my.name_bangla);
    printf("Name : %s\n", my.name_eng);
    printf("father : %s\n", my.f_name);
    printf("mother : %s\n", my.m_name);
    printf("DOB : %d/%s/%d\n", my.s.date,my.s.month,my.s.year);
    printf("ID : %d", my.ID);
}
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
N Tahseen
  • 31
  • 5
  • None of the `char *` members point to any allocated memory. You are simply writing to uninitialized pointers and crashing. – user16217248 Sep 12 '22 at 16:26
  • Using **fflush** on **stdin** is a UB. – CGi03 Sep 12 '22 at 17:20
  • Despite its popularity and apparent simplicity, the `scanf` function is actually a strange and persnickety beast. You don't need extra spaces in `" %s"` like you've got, but you *do* need one in `"%[^\n]"`. You might want to read these [secret undocumented rules](https://stackoverflow.com/questions/72178518#72178652) for using `scanf` more safely. – Steve Summit Sep 12 '22 at 18:37

1 Answers1

1

This line creates an uninitialized object:

struct NID my;

Proceeding to write to the uninitialized pointers in the structure is very wrong. These are wild pointers that almost certainly do not point to memory allocated to the program. These access violations crash your program.

By declaring these fields as arrays by guessing a maximum size (This example uses 16 but you can adjust them to your needs), proper memory is allocated when declaring the variable:

struct DOB
{
        int date;
        char month[16];
        int year;
};
struct NID
{
        char name_bangla[16];
        char name_eng[16];
        char f_name[16];
        char m_name[16];
        struct DOB s;
        int ID;
};

Also make sure the scanf calls do not exceed the allocated memory by adding a limit to the format:

scanf("%16s", my.name_bangla);
scanf("%16s", my.name_eng);
scanf("%16s", my.f_name);
// Etc
user16217248
  • 3,119
  • 19
  • 19
  • 37
  • i didn't get the uninitialized object part. how is "struct NID my;" creating uninitialized object?isn't it simple variable declaration? – N Tahseen Sep 14 '22 at 08:37
  • @NTahseen You are creating it, but you are not assigning any value to it. To initialize all members to 0/NULL, use `struct NID my = {0};`. However, if you are only writing values to it, you do not need to initialize it. – user16217248 Sep 14 '22 at 18:41
  • i am taking input from user for all the members (my.name_bangla,my.name_eng, my.f_name, my.m_name) of struct NID my. So, why will I need to initialize it? – N Tahseen Sep 16 '22 at 19:00
  • @NTahseen You don't. – user16217248 Sep 16 '22 at 19:02