How to fix this lvalue warning?

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 "students" are underlined, saying that the expression must be a modifiable lvalue value. 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; 
+4
source share
6 answers

Using array

You say that you have:

 typedef struct person { char names[20][10]; char sex[6][10]; int ages[10]; int month[10]; int day[10]; int year[10]; } person; 

No need for [10] . You already have this ad person student[10] , which is the right place for [10] . Remove extraneous arrays:

 typedef struct person { char name[20]; char sex[6]; int age; int month; int day; int year; } person; 

Line processing

Also your lines are not null terminated . In C lines, you must have an extra character '\0' at the end to indicate where the end of the line is. For example, your naming should be:

 student[0].name[0] = 'C'; student[0].name[1] = 'a'; student[0].name[2] = 'm'; student[0].name[3] = 'i'; student[0].name[4] = '\0'; 

In fact, there is an easier way to assign a line than to make it a character at a time. The strcpy function copies an entire line at a time:

 strcpy(student[0].name, "Cami"); 

Or the easiest option is to use a string class available in C ++. This makes string processing much easier than C-manipulating character arrays. With the string class, your code will look like this:

 // Modified struct declaration. typedef struct person { std::string name; std::string sex; int age; // ... } person; // Modified assignment. student[0].name = "Cami"; 
+9
source

Why did you define names as a 2D array with 10 columns? Do you think you need to do this because you are going to create 10 person objects?

If this happens, keep this in mind: when creating a person structure, you just need to define member variables for this single object .

So, if your person has only one name and one gender (which requires 6 characters), just use this because when you create 10 objects of a person ( person student[10]; ), you create 10 unique copies of the person object ! So person[0].name is different from person[1].name .

So, first define the member variables in person for one person:

 struct person { char names[20]; char sex[6]; int age; int month; int day; int year; }; 

And then assign the characters to the name as you indicated in your code.

Now this is not a great idea, because names not initialized. Thus, student[0].names[4] is undefined (when it must have a null character to complete it). Do you consider using std::string ? You have C ++ in the end!

Also, you will notice that I have changed the definition of person ('struct person ... instead of typedef .. `), as that is all you need to do in C ++.

Changes:

Consider the following person structure:

 #include <string> struct person { std::string person; std::string sex; int age; int month; int day; int year; }; 

Now you can assign a line simply:

 person student[10]; student[0].name = "Cami"; student[0].sex = "Female"; student[0].age = 16; student[0].month = 8; student[0].day = 2; student[0].year = 1993; 

And suppose you want to fill it with a for loop using keyboard input --- now it's a lot easier:

 #include <iostream> for(int i = 0; i < 10; ++i) { std::cin>>student[i].name; std::cin>>student[i].sex; std::cin>>student[i].age; std::cin>>student[i].month; std::cin>>student[i].day; std::cin>>student[i].year; } 
+2
source

student[0].names[0] is of type (char *) and cannot be assigned.

You need to assign student[0].names[0][0] (or change the definition of names )

+2
source

An irreparable lvalue addressable but not assignable.

Having said that, do you plan for the struct be a container for a bunch of different names?

You really need a struct array to create a container for people. (ie, struct person p[10]; )

+1
source

You do not need these additional [10] for each of the fields of the person structure. This makes these fields their own arrays. You want, for example, an array of ten people, each of whom has an age. What you have now is an array of ten people, each of which has ten ages.

Each element of the student array has its own instance of each of the fields defined in person , so the fields themselves should not be arrays.

+1
source
  • If person not listed in this file, you may not include the corresponding header file.

  • Remember to add \0 to your lines

  • If the names are a 2d array, there should not be:

    names [0] [0] = 'J';
    names [0] [1] = 'i';
    names [0] [2] = 'm';
    names [0] [3] = '\ 0';

0
source

Source: https://habr.com/ru/post/1316333/


All Articles