0

I have a problem in which there is a struct that will hold the name, age and student id. I have to take an input from the user and make that number of structs without using any array notation. And then each of the struct's parameters should be taken input from the user and printed at the same time. It is like creating a database of students. The struct is:

typedef struct
{
char *name;
char *std_id;
int age;
} student;

So my input would be like :

Number of students: 3
Name of the student1: Bro
std_id of the student1: 46845
age of the student1: 18

Name of the student2: kim
std_id of the student2: 46867
age of the student2: 19

Name of the student3: Sean
std_id of the student3: 46862
age of the student3: 18

And the output would be like:

Name of the student1 is: Bro
std_id of the student1 is: 46845
age of the student1 is: 18

Name of the student2 is: kim
std_id of the student2 is: 46867
age of the student2 is: 19

Name of the student3 is: Sean
std_id of the student3 is: 46862
age of the student3 is: 18

The main problem is we can't use any array in this problem.

What I tried coding the problem by searching the internet is this code:

#include<stdio.h>
#include<string.h>


typedef struct
{
    char *name;
    char *std_id;
    int age;
} students;


int main()
{

    int num;
    printf("Type the number of students:");
    scanf("%d", &num);

    students* ptr = malloc(num * sizeof(*ptr));
    if(ptr == NULL)
    {
        printf("memory not free!");
        return 0;
    }


    for(int i=0; i<num; i++)
    {

        printf("\nGive the name of the std %d:", i+1);
        (ptr+i)->name = malloc(sizeof(char)*20);
        if((ptr+i)->name == NULL)
        {
            printf("memory not free!");
            return 0;
        }
        scanf("%s", (ptr+i)->name);

        printf("Give the std_id of the std %d:", i+1);
        (ptr+i)->std_id = malloc(sizeof(char)*10);
        if((ptr+i)->std_id == NULL)
        {
            printf("memory not free!");
            return 0;
        }
        scanf("%s", (ptr+i)->std_id);

        printf("Give the age of std %d:", i+1);
        scanf("%d", (ptr+i)->age);


    }

    for(int i=0; i<num; i++)
    {

        printf("\nThe name of the std %d: %s", i+1, (ptr+i)->name);
        printf("\nThe std_id of the std %d: %s", i+1, (ptr+i)->std_id);
        printf("\nThe age of the std %d: %d", i+1, (ptr+i)->age);
    }


    return 0;
}

Using this code, this is what my console looks like:

Type the number of students:3

Give the name of the std 1:Sean
Give the std_id of the std 1:45
Give the age of std 1:23

Process returned -1073741819 (0xC0000005)   execution time : 23.131 s
Press any key to continue.

Here I am trying to first run the code for name variable. If that runs correctly, I'll implement the other variables like name variable. But when I run this code the the program exits after taking just one input. I just can not figure out the problem by my own. Any help on how to tackle this problem will be highly appreciated.

User 1999
  • 13
  • 6
  • 1
    `(ptr+i)->name` is the same as `ptr[i]->name`. – Zakk Jan 31 '23 at 08:41
  • 3
    "without using any array notation" This is a nonsense requirement. See [Do pointers support "array style indexing"?](https://stackoverflow.com/questions/55747822/do-pointers-support-array-style-indexing) – Lundin Jan 31 '23 at 08:44
  • No arrays? Sounds like a "linked-list" would be an easy solution. – Fe2O3 Jan 31 '23 at 08:47
  • 1
    PS: Think about the info contained in one instance of the struct: it's ONE student, right? Please don't use plural variable names or type names for singular instances. That only makes the code more difficult to read. – Fe2O3 Jan 31 '23 at 08:49
  • "The main problem is we can't use any array in this problem." : so you maybe need a linked list. This kind of problem can be solved properly only with arrays or dynamic memory allocation (which is pretty much the same thing as arrays) or with linked lists. We need more information. – Jabberwocky Jan 31 '23 at 09:00
  • @Zakk no `(ptr+i)->name` is the same as `ptr[i].name`, you need a dot after `ptr[i]`, not the arrow. – mch Jan 31 '23 at 10:11
  • @mch Yes... That was a copy/paste typo. – Zakk Jan 31 '23 at 10:34
  • the immediate problem is you forgot to write `&` when calling scanf – user253751 Jan 31 '23 at 10:38

1 Answers1

1

students* ptr = malloc((sizeof(int) + sizeof(char) * 50) * num);

This doesn't make any sense at all and I don't even understand what you are trying to do. If you wish to allocate an array of structs then you have to do:

students* ptr = malloc(num * sizeof(*ptr));

or equivalent:

students* ptr = malloc( sizeof(students[num]) );
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Well I was confused about the size of the `*ptr` as the char pointers may have variable length, I set the of each struct can use maximum 50 characters. Can you look at the edit please? – User 1999 Jan 31 '23 at 08:55
  • 3
    @User1999 What edit, you didn't change anything related to this. And also please _don't_ edit the post to change the code once there are answers posted or your changes might invalidate those answers. – Lundin Jan 31 '23 at 09:01