0

In c language for this question need pointer however i cant understand how can i link with course and student struct with pointer.program expected AddCourse operation and take course id and student id and again loop addcourse again again if i write exit, stop loop and print students taken course and struct info. this is the question:

structures must be filled by user inputs. After filling your structures, you will let user to get any student enroll to any course from the course list. Hint: To achieve these steps, you must use pointer in your student structure to link it to course structure"

i add photos for the expected program output please open this two images and two images are dependented.

1.photo 2.photo

and its my incomplete code because how can i use to link two struct with pointer.

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

struct student
{
    char name[50],surname[50];
    int  id,year;
};

struct Courses{
    char course_name[50];
    int course_id,course_credit;
};

int main(){
    struct student std[2];
    struct Courses crs[4];
    
    printf("enter information of students:\n");
    for(int i=0; i<2;i++){
        printf("Enter student id:");
        scanf("%d",&std[i].id);
        printf("Enter first name:");
        scanf("%s",std[i].name);
        printf("Enter last name:");
        scanf("%s",std[i].surname);
        printf("Enter year:");
        scanf("%d",&std[i].year);
        printf("\n");
    }
    printf("Enter information of courses:\n");
    for (int i = 0; i < 4; i++)
    {
        printf("course id:");
        scanf("%d",&crs[i].course_id);
        printf("course name:");
        scanf("%s",&crs[i].course_name);
        printf("course credit:");
        scanf("%d",&crs[i].course_credit);
        printf("\n");
    }
    printf("-Displaying Courses-\n");
    for(int i=0; i<4;i++){
        printf("course id:%d\n",crs[i].course_id);
        printf("course name :%s\n",crs[i].course_name);
        printf("course credit:%d",crs[i].course_credit);
        printf("\n");
    }
    printf("-Display Students-\n");
    for(int i=0; i<2;i++){
        printf("student id:%d\n",std[i].id);
        printf("student name and surname:%s %s\n",std[i].name,std[i].surname);
        printf("year:%d",std[i].year);
        printf("\n");
    }
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
DarkSpy
  • 15
  • 1
  • 5
  • 3
    Easiest way would be some kind of linked-list structure you can chain off of `student`. – tadman Oct 19 '20 at 14:10
  • Tip: Try and keep your case conventions consistent. It's odd seeing lower-case `student` and upper-case `Courses`. It's additionally odd that "Courses" (plural) represents one course. – tadman Oct 19 '20 at 14:11

1 Answers1

0

You need a third data structure to link a student and course.

As mentioned by @tadman, a linked list is one type of data structure that can be used for this. See How to implement a linked list in C? for one version of an implementation.

In addition, https://en.wikipedia.org/wiki/Data_structure lists some other commonly used ones. The one you should choose will depend on your requirements (e.g. sorting, inserting/removing, etc.).

In addition to your question, here is a short (non-exhaustive) list of some things for you to consider based on the code you posted:

  1. Use another data structure for 'std' and 'crs' besides an array. In this data structure, you could have helper functions that encapsulate many of the lines in your current main function (e.g. printCourses())

  2. You have possible unintentional and undefined behavior with your use of the scanf function. See Disadvantages of scanf and How to read / parse input in C? The FAQ.

  3. Instead of hardcoding array sizes (e.g. name[50]), use something like #define MAX_NAME_LENGTH 50 and name[MAX_NAME_LENGTH]