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.

8
  • 1
    (ptr+i)->name is the same as ptr[i]->name. Commented Jan 31, 2023 at 8:41
  • 3
    "without using any array notation" This is a nonsense requirement. See Do pointers support "array style indexing"? Commented Jan 31, 2023 at 8:44
  • No arrays? Sounds like a "linked-list" would be an easy solution. Commented Jan 31, 2023 at 8: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. Commented Jan 31, 2023 at 8: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. Commented Jan 31, 2023 at 9:00

1 Answer 1

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]) );
Sign up to request clarification or add additional context in comments.

2 Comments

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?
@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.

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.