DMA + Structure Problem

#include <stdio.h>
#include <stdlib.h>
/*
Problem: 
To take n inputs of subject along with their marks followed by \n using structures and Dynamic memory allocation.
*/
struct data{
    char subject[50];    
    int marks;
}; 
int main(){
    int n;
    printf("Enter the number of records: ");
    scanf("%d",&n);
    struct data *info;             //no need to put [size] if * used.
    info=(struct data*)calloc(n,sizeof(struct data));

    for(int i=0;i<n;i++){
        printf("%d.Enter name of Subject and Marks respectively :\n",i+1);
        scanf("%s\n%d",&info[i].subject,&info[i].marks);
    }
    printf("Displaying Information: \n");
    for(int i=0;i<n;i++){
        printf("-----------------------------------\n");
        printf("%s\t%d\n",info[i].subject,info[i].marks);
    }
    getchar();
    getchar();
    return 0;
}
Output

Comments

Post a Comment

Popular Posts