extern int ID; // student ID number
struct personalDetails det; // pD
struct classRecords rec; // cR
struct student st1; // student 1
struct student st2; // student 2
struct student st3; // student 3
struct student st4; // student 4
struct student st5; // student 5
char pD[30]; // for storing values that will be assigned to personalDetails
double cR; // for storing values that will be assigned to classRecords
struct student studentID(int id) { // identifies student by ID
if (id == 1) {
struct student x = st1;
return x;
}
else if (id == 2) {
struct student x = st2;
return x;
}
else if (id == 3) {
struct student x = st3;
return x;
}
else if (id == 4) {
struct student x = st4;
return x;
}
else if (id == 5) {
struct student x = st5;
return x;
}
}
我需要函數updateName()爲'name'賦值,該值是另一個struct personalDetails中的值。如何爲通過函數返回的結構賦值C
void updateName() {
printf("Enter student's name\n");
scanf("%s", pD);
studentID(ID).det.name = pD;
我目前收到此錯誤:
operations.c: In function ‘updateName’:
operations.c:55:24: error: lvalue required as left operand of assignment
studentID(ID).det.name = pD;
請告訴我,我怎麼能提前解決這個問題,謝謝!
編輯1:這是有興趣的結構defs。
struct personalDetails {
char *name;
char *phoneNum;
char *address;
};
struct classRecords {
double assignment;
double midterm;
double finalMark;
double total;
};
struct student{
struct personalDetails det;
struct classRecords rec;
};
感謝您的答案,strcpy的擺脫了編譯錯誤的,但現在當我的「名字」輸入值,我收到了「分段錯誤」。有人知道爲什麼
'typedef'是你的朋友;) –