简体   繁体   English

如何将数据存储在也是指针的结构成员中?

[英]How to store data in struct member which is also a pointer?

This is the struct that I have to use这是我必须使用的结构

struct subject {  
char subjectID[10];  
float marks; }; 

struct student {  
char name[20];  
char ID[10];  
struct subject *enrolled; }; 

My program has to let the students enroll in more than one subject, how do I store this???我的程序必须让学生注册一个以上的科目,我如何存储这个???

Consider adding 2 elements to your structure student .考虑在结构student中添加 2 个元素。 Add a dynamic array of subject and a enrolled_count and using that track enrolled subjects.添加subject的动态数组和enrolled_count并使用该跟踪注册主题。 An example:一个例子:

struct Student
{
    char name[20];
    char ID  [10];
    struct subject *enrolled;
    int enrolled_count;
}

So when you access it somewhere you can use Student.enrolled[index] (the. can be -> depends if it's a pointer to a struct)因此,当您在某处访问它时,您可以使用 Student.enrolled[index] (可以是 -> 取决于它是否是指向结构的指针)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM