[英]Store the same string in two different struct types
I'm working on structures, and I need to assign a string in a char array. 我正在研究结构,需要在char数组中分配一个字符串。 Here is my some spesific parts of my code:
这是我代码中一些特殊的部分:
typedef struct{
int std_id;
char std_name[STD_NAME_SIZE];
double gpa;
char courses[COURSE_SIZE];
}student_t;
typedef struct{
int course_id;
char course_name[COURSE_NAME_SIZE];
char course_code[COURSE_CODE_SIZE];
char course_acronym[COURSE_ACRONYM_SIZE];
int quota;
}course_t;
this is the struct that I use. 这是我使用的结构。 and this is the assignment part:
这是分配部分:
for(int i = 0; i < COURSE_SIZE; i++){
printf("Enter course name: ");
scanf("%s", &courseDetails.course_name);
studentProfile.courses[i] = courseDetails.course_name;
}
for(int j = 0; j < COURSE_SIZE; j++){
printf("\nYour %d course is: %s", j + 1, studentProfile.courses[j]);
}
I think the problem happens at studentProfile.courses[i] = courseDetails.course_name
part. 我认为问题发生在
studentProfile.courses[i] = courseDetails.course_name
部分。 Should I use pointers? 我应该使用指针吗? How can I solve this problem ?
我怎么解决这个问题 ?
Presumably, COURSE_SIZE is defined as the maximum number of courses that a student can take. 大概,COURSE_SIZE定义为学生可以参加的最大课程数。 Then in the student_t struct,
courses
would be declared as char courses[COURSE_SIZE][COURSE_NAME_SIZE]
and it would be set as strcpy(studentProfile.courses[i], courseDetails.course_name);
然后在student_t结构中,将
courses
声明为char courses[COURSE_SIZE][COURSE_NAME_SIZE]
,并将其设置为strcpy(studentProfile.courses[i], courseDetails.course_name);
But that is still not the correct way to do it. 但这仍然不是正确的方法。 What you really want is the
courses
array to be an array of courses: 您真正想要的是将
courses
数组设置为courses
数组:
course_t *courses[COURSE_SIZE];
and set courses[i] to point to some course j: 并设置课程[i]指向课程j:
studentProfile.courses[i] = &courseDetails[j];
then if you want to refer to the name of the ith course taken by that student you would use courses[i]->course_name
. 那么,如果您想参考该学生选择的第i门课程的名称,则可以使用
courses[i]->course_name
。
Note that you will have to declare the course_t struct before you declare the student_t struct. 注意,在声明student_t结构之前,必须先声明course_t结构。
You need to make the courses
field an array of pointers to char
. 您需要使
courses
字段为一个指向char
的指针数组。 Also, courseDetails
needs to be an array: 另外,
courseDetails
必须是一个数组:
#include <stdio.h>
#include <stdlib.h>
#define STD_NAME_SIZE 81
#define COURSE_SIZE 3
#define COURSE_NAME_SIZE 81
#define COURSE_CODE_SIZE 81
#define COURSE_ACRONYM_SIZE 81
typedef struct {
int std_id;
char std_name[STD_NAME_SIZE];
double gpa;
char *courses[COURSE_SIZE]; // Make this an array of pointers
} student_t;
typedef struct{
int course_id;
char course_name[COURSE_NAME_SIZE];
char course_code[COURSE_CODE_SIZE];
char course_acronym[COURSE_ACRONYM_SIZE];
int quota;
} course_t;
int main(void)
{
student_t studentProfile;
course_t courseDetails[COURSE_SIZE];
for (int i = 0; i < COURSE_SIZE; i++){
printf("Enter course name: ");
scanf("%s", courseDetails[i].course_name);
studentProfile.courses[i] = courseDetails[i].course_name;
}
printf("\n");
for (int j = 0; j < COURSE_SIZE; j++){
printf("Your %d course is: %s\n", j + 1, studentProfile.courses[j]);
}
return 0;
}
Compiled With 编译与
gcc -Wall -Werror -o scratch scratch.c
Example Input and Output 输入和输出示例
Enter course name: first
Enter course name: second
Enter course name: third
Your 1 course is: first
Your 2 course is: second
Your 3 course is: third
Note 注意
This approach is very fragile because the string pointers in studentProfile
point to strings stored in other variables. 这种方法非常脆弱,因为
studentProfile
的字符串指针指向存储在其他变量中的字符串。 I understand that this is a toy program that is part of some kind of programming homework. 我了解这是一个玩具程序,属于某种编程作业的一部分。 However, I just want to caution anyone against using this code in a production environment.
但是,我只想警告任何人,不要在生产环境中使用此代码。 In a real program, you would probably just store an array of
course_id
values in the student_t
. 在实际程序中,您可能只将一个
course_id
值数组存储在student_t
。 To get the course name, you would first find the course_t
with the matching course_id
and then display the course_name
. 要获得课程名称,你会先找到
course_t
与匹配course_id
,然后显示course_name
。
Since you're using not using pointers, you should use something like 由于您不使用指针,因此应使用类似
strcpy(studentProfile.courses[i], courseDetails.course_name);
EDIT: this is obviously wrong, as comment points out. 编辑:正如评论所指出的,这显然是错误的。
maybe you just forgot array access for the other variable, as in 也许您只是忘记了对另一个变量的数组访问,例如
courseDetails.course_name[i]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.