As stated in question comments, you need to allocate memory for ALL subject
members, not just the first:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct course {
int marks;
char *subject;
};
int main(int argc, char *argv[])
{
struct course *ptr;
int i, noOfRecords;
printf("Enter the number of records: ");
scanf("%d", &noOfRecords);
//Memory allocation for noOfRecords structures
ptr = (struct course *)malloc(noOfRecords * sizeof(struct course));
for (i = 0; i < noOfRecords; ++i) {
printf("Enter the name of the subject and marks respectively:\n");
(ptr + i)->subject = (char*) malloc(20*sizeof(char));
scanf("%s %d", (ptr + i)->subject, &((ptr + i)->marks));
}
printf("Displaying Information:\n");
for (i = 0; i < noOfRecords; ++i) {
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);
}
return 0;
}
Also, I suggest you to:
- Use a
#define
for subject max length
- Always check user input or limit it (truncate or force to retype). You might get unexpected behaviors if user inserts more than 19 chars plus
\0
for subject.
- Use
typedef struct {...} course;
. It will be much easier to handle arrays.
- Remember to free the memory you claimed.
As a result:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// From https://stackoverflow.com/a/17387990/2928168
#define MAX_SUBJECT "20"
typedef struct {
int marks;
char *subject;
} Course;
int main(int argc, char *argv[])
{
Course *courses;
int i, noOfRecords;
printf("Enter the number of records: ");
scanf("%d", &noOfRecords);
// Memory allocation for noOfRecords structures
courses = (Course *) malloc(noOfRecords * sizeof(Course));
for (i = 0; i < noOfRecords; ++i) {
printf("Enter the name of the subject and marks respectively:\n");
courses[i].subject = (char*) malloc(atoi(MAX_SUBJECT)*sizeof(char));
scanf("%" MAX_SUBJECT "s %d", courses[i].subject, &(courses[i].marks));
//fgets(courses[i].subject, sizeof(buf), stdin);
}
printf("Displaying Information:\n");
for (i = 0; i < noOfRecords; ++i) {
printf("%s\t%d\n", courses[i].subject, courses[i].marks);
free(courses[i].subject);
}
free(courses);
return 0;
}
Edit
You can even use the trick from https://stackoverflow.com/a/6671729/2928168 to increase performance:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SUBJECT 20
#define Q(x) #x
#define QUOTE(x) Q(x)
#define MAX_SUBJECT_STR QUOTE(MAX_SUBJECT)
typedef struct {
int marks;
char *subject;
} Course;
int main(int argc, char *argv[])
{
Course *courses;
int i, noOfRecords;
printf("Enter the number of records: ");
scanf("%d", &noOfRecords);
// Memory allocation for noOfRecords structures
courses = (Course *) malloc(noOfRecords * sizeof(Course));
for (i = 0; i < noOfRecords; ++i) {
printf("Enter the name of the subject and marks respectively:\n");
courses[i].subject = (char*) malloc(MAX_SUBJECT*sizeof(char));
scanf("%" MAX_SUBJECT_STR "s %d", courses[i].subject, &(courses[i].marks));
//fgets(courses[i].subject, sizeof(buf), stdin);
}
printf("Displaying Information:\n");
for (i = 0; i < noOfRecords; ++i) {
printf("%s\t%d\n", courses[i].subject, courses[i].marks);
free(courses[i].subject);
}
free(courses);
return 0;
}