My code is:
void main() {
person student[10];
student[0].names[0] = 'C';
student[0].names[1] = 'a';
student[0].names[2] = 'm';
student[0].names[3] = 'i';
student[0].ages = 16;
student[0].sex[0] = 'F';
student[0].sex[1] = 'e';
student[0].sex[2] = 'm';
student[0].sex[3] = 'a';
student[0].sex[4] = 'l';
student[0].sex[5] = 'e';
student[0].month = 8;
student[0].day = 2;
student[0].year = 1993;
}
All of the "student" is underlined saying expression must be a modifiable lvalue. How can i fix this?
person
typedef struct person
{
char names[20][10];
char sex[6][10];
int ages[10];
int month[10];
int day[10];
int year[10];
} person;
Array usage
You say you have:
There’s no need for the
[10]‘s. You already have that in theperson student[10]declaration, which is the proper place for the[10]. Remove the extraneous arrays:String handling
Also your strings aren’t null-terminated. In C strings need to have an extra
'\0'character at the end to indicate where the end of the string is. Your name assignment, for example, should be:Actually though, there’s an easier way to assign to a string than to do it a character at a time. The strcpy function will copy an entire string in one go:
Or, the easiest option of all is to use the string class available in C++. It makes string-handling a whole lot easier than the C way of manipulating character arrays. With the string class your code would look like this: