Froobyflake Froobyflake - 11 days ago 5
C Question

Creating an array of structures

I am trying to create an array of structures containing some basic information. My code is as follows:

typedef struct {
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip;
char phone[15];
int accountId;
} Customer;

int main(void) {

int i, customer_number, _zip, _accountId;
struct Customer customer_list[9];
char _firstName[30], _lastName[30], _street[35], _city[20], _state[3], _phone[15];


for (i = 0; i < 10; i++) {
customer_number = 0;
printf("Enter data for customer %d: \n", customer_number);
printf("Enter First Last Phone: ");
scanf("%s%s%s", &_firstName, &_lastName, &_phone);

printf("\nEnter Address (Street City State ZIP): ");
scanf("%s%s%s%d", &_street, &_city, &_state, &_zip);

Customer customer_list[i] = {[i].firstName = _firstName}

}

return 0;
}


It seems like this should work, however I am getting an error saying " i must have constant value" Can anyone push me in the right direction? Thank you!

Answer
Customer customer_list[i] = {[i].firstName = _firstName}

This line needs to be

strcpy(customer_list[i].firstName, _firstName);

EDIT: assignment should be done by strcpy()