1

By using this code I can record only one student information, but what if I want to record thousand or more information of students ?

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

struct {
  char name[10];
  int id;
  float marks;
  char grade;
} s;

int main() {

  printf("Name : ");
  scanf("%s", s.name);

  printf("Id : ");
  scanf("%d", &s.id);

  printf("Marks : ");
  scanf("%f", &s.marks);

  printf("Grade : ");
  scanf(" %c", &s.grade);

  printf("%s %d %.2f %c\n", s.name, s.id, s.marks, s.grade);
}
Jeeremy
  • 23
  • 4
  • 3
    Use array and loops – P.W Oct 12 '18 at 07:34
  • Then you do the same thing a thousand or more times, for example, with a loop. Do you have some specific problem doing this we can help with? – Sami Kuhmonen Oct 12 '18 at 07:34
  • First you should name your struct.... Afterwards you can use an array of type your struct – Thomas Oct 12 '18 at 07:36
  • There are *many, many* examples on SO about creating a fixed or dynamic array of structs. Two examples: [how to create array of struct](https://stackoverflow.com/questions/49606737/how-to-create-array-of-struct/49609210#49609210) and [malloc dynamic array in dynamic array of structs](https://stackoverflow.com/questions/41170830/malloc-dynamic-array-in-dynamic-array-of-structs/41172310#41172310). – David C. Rankin Oct 12 '18 at 08:36
  • If by any chance the amount of data is so large that you can't put them all in memory, consider using a database. Otherwise array is the way to go. – colin Oct 12 '18 at 08:39

4 Answers4

2

You can consider using dynamic arrays, if length of students reach max, realloc students

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

typedef struct student {
    const char* name;
    int id;
    float marks;
    char grade;
} student;

typedef struct class {
    int len;
    int max_alloc_len;
    student** students;
} class;


student* newClass() {
    struct class* c;
    c = malloc(sizeof(c));
    c->len = 0;
    c->max_alloc_len = 10;
    c->students = malloc(sizeof(struct student*) * 10);
    return c;
}

student* newStudent(const char* name)
{
    struct student* s;
    s = malloc(sizeof(s));
    s->name = name;
    return s;
}

void addStudent(class* c, student* s)
{
    c->students[c->len++] = s;
    if (c->len >= c->max_alloc_len) {
        c->max_alloc_len *= 2; 
        c->students = realloc(c->students, sizeof(struct student*) * c->max_alloc_len);
    }
}

int main() {
    int i;
    class* c;
    student* s;

    c = newClass();
    for (i = 0; i < 20; i++) {
        s = newStudent("jim");
        addStudent(c, s);
    }

    printf("%s", c->students[19]->name);
}
sundb
  • 490
  • 2
  • 8
0

You can create one array of structure and store them like how we store elements into array. See the complete code given below:

#include <stdio.h>
#include <string.h>
#define N 100

struct record{
  char name[10];
  int id;
  float marks;
  char grade;
} ;

int main() {
  struct record s[N]; // allocate memory for N=100 records
  int num_records= 0;


  printf("How many records do you wish to store (1- 100): ");
  scanf("%d", &num_records);
  while (num_records > N)
  {
    printf("Enter between than (1- 100): ");
    scanf("%d", &num_records);
  }


  for (int i= 0; i < num_records; i++)
  {
  printf("\n____Enter details for Record %d _____\n",i+1); // now records starts from 1 insted of f0
  printf("Name : ");
  scanf("%s", s[i].name);

  printf("Id : ");
  scanf("%d", &s[i].id);

  printf("Marks : ");
  scanf("%f", &s[i].marks);

  printf("Grade : ");
  scanf(" %c", &s[i].grade);
  }


  for (int i= 0; i < num_records; i++)
    printf("%s %d %.2f %c\n", s[i].name, s[i].id, s[i].marks, s[i].grade);
}

Hope this helps you.

Akhilesh Pandey
  • 868
  • 8
  • 18
0
#include <stdio.h>
#include <stdlib.h>

struct{
  char name[10];
  int id;
  int marks;
  char grade;
}s[10];
int main()

{
    int i;
    for(i=0;i<sizeof(s);i++){
    printf("name: ");
    scanf("%s",s[i].name);
    printf("id: ");
    scanf("%d",&s[i].id);
    printf("marks: ");
    scanf("%d",&s[i].marks);
    if(s[i].marks>=90)
        s[i].grade='S';
    else if(s[i].marks>=80)
        s[i].grade='A';


    printf("\n%s %d %d %c\n",s[i].name,s[i].id,s[i].marks,s[i].grade);
}
    return 0;
}

This way you can add as many records as you want.

user10493493
  • 3
  • 1
  • 6
0

If you don't know the number of students at the time of coding this program you can always use linked lists and malloc to allocate memory dynamically although it's not safe especially if you have very limited memory resources for example in embedded systems case. your code will be

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

struct LinkedList{
  char name[10];
  int id;
  float marks;
  char grade;
  struct LinkedList *next;
};
typedef struct LinkedList *node; //Define node as a pointer of data type struct LinkedList

node createNode(){
    node temp; // declare a node
    temp = (node)malloc(sizeof(struct LinkedList)); // allocate memory using malloc()
    temp->next = NULL;// make next point to NULL
    return temp;//return the new node
}
node addNode(node head,char* name, int id, float marks, char grade){
    node temp,p;// declare two nodes temp and p
    temp = createNode();//createNode will return a new node with data = value and next pointing to NULL.
    strncpy(temp->name, name, 10); // add element's data part of node
    temp->id = id; // add element's data part of node
    temp->marks = marks; // add element's data part of node
    temp->grade = grade; // add element's data part of node
    if(head == NULL){
        head = temp;     //when linked list is empty
    }
    else{
        p  = head;//assign head to p 
        while(p->next != NULL){
            p = p->next;//traverse the list until p is the last node.The last node always points to NULL.
        }
        p->next = temp;//Point the previous last node to the new node created.
    }
    return head;

}
char i, x;
node head; // declare The head
char name[10];
int id;
float marks;
char grade;
int main() {

    printf("If you want to quit press q ,to continue press anything else");
    i = getch();

    while(i !='q'){
        printf("\n Name : ");
        fgets(name, 10, stdin);

        printf("\n Id : ");
        scanf("%d", &id);

        printf("\n Marks : ");
        scanf("%f", &marks);

        printf("\n Grade : ");
        scanf(" %c", &grade);

        printf("\n name:  %s id: %d marks: %.2f grade: %c\n", name, id, marks, grade);
        addNode(head, name, id, marks, grade);
        x = 'y';
        do{
           printf("\n If you want to quit press q ,to continue press anything else");
           i = getch();
           if(i=='q'){ 
                printf("\n Are you sure you want to quit?");
                printf("\n press y: for yes anything else: for NO");
                x = getch();
           }
        }while(x !='y');
    }
    return 0;
}