Structure question

I am looking into structures in C++. With the following small program, I am getting an error during compile that says:

Lvalue required in function main() three times.

Here is the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// temp struct

#include <iostream>
using namespace std;

	struct person {
		char firstName[40];
		char lastName[40];
		int age;
		long address;
		char street[40];
	};


int main()
{
	person firstPerson;
	firstPerson.firstName = "Tom";
	firstPerson.lastName = "Smith";
	firstPerson.age = 35;
	firstPerson.address = 42957;
	firstPerson.street = "Woodland";

	return 0;
}


How can I assign the above values to the struct fields firstName, lastName, and street?

Thank you. :)
You use strcpy like this strcpy(firstPerson.firstName, "Tom");

or make your life easier by using std::string instead of char arrays
Better to use strncpy than just strcpy, as it allows you specify the size of the buffer you're copying to.

http://www.cplusplus.com/reference/clibrary/cstring/strncpy/

1
2
strcpy(firstPerson.firstName, "Tom", 39);
// 1 less than buffer len, to ensure space fpr null 

Thanks for the responses Peter and Andy.
Last edited on
Topic archived. No new replies allowed.