// Class activity solution #include #include #include struct _course { int student_id; int marks; char subject[20]; //char *subject; }; typedef struct _course course; int main() { // Task1: Create a pointer variable ptr for the structure course course * ptr; int i, no_of_records; printf("Enter number of records: "); scanf("%d", &no_of_records); // TODID Task 2: Allocate the memory for no_of_records structures with pointer ptr pointing to the base address. ptr = calloc(no_of_records, sizeof(course)); // Iterate through the no_of_records to get information from the user for each record for(i = 0; i < no_of_records; ++i) { printf("Enter the student id,name of the subject and marks respectively:\n"); //TODID Task 3: Get the info id, name of the subject followed by the student mark /*course tempcourse; scanf("%d %s %d", &tempcourse.student_id, tempcourse.subject, &tempcourse.marks ); ptr[i].student_id = tempcourse.student_id; ptr[i].marks = tempcourse.marks; strcpy(ptr[i].subject, tempcourse.subject);*/ //scanf("%d %s %d", &ptr[i].student_id, ptr[i].subject, &ptr[i].marks ); course tempcourse; scanf("%d %s %d", &tempcourse.student_id, tempcourse.subject, &tempcourse.marks ); // ptr[i] = tempcourse; // changing what ptr[] is pointing to BAD memcpy(&ptr[i], &tempcourse, sizeof(course)); // Copy into the allocated memory } // If stuff happened here we would get strange errors printf("Displaying Student Information:\n"); for(i = 0; i < no_of_records ; ++i) { printf("Student ID: %d\n",(ptr+i)->student_id); printf("%s\t%d\n", (ptr+i)->subject, (ptr+i)->marks); } free(ptr); return 0; }