Search code examples
carraysvisual-studiostructlvalue

Accessing a member of a structure in an array of structures which is a member of an array of different structures


I have a structure called Object defined as:

    struct Object
    {
        char name[20];
        char description[50];
    };

I also have another structure called Room defined as:

    struct Room
    {
        int number;
        char description[50];
        struct Object objects[10];
    };

I then initialise an array of Room and attempt to change the name of an object in a room:

    void main()
    {
        struct Room rooms[1][1];
        rooms[0][0].objects[0].name = "Cabinet";
    }

The problem I have is that visual studio gives me these errors:

    rooms[0][0].objects[0].name = "cabinet";

    Error1:
        expression must be a modifiable lvalue

    Error2:
        '=': left operand must be l-value

I am using visual studio 2015 community edition's C++ compiler to compile and run C code.

I'm not sure if what i'm trying to do is possible in C, the idea is that i have 4 rooms, each room has 11 objects and each object can have a name and a description.

Paste bin to the full code http://pastebin.com/jQJekLk9


Solution

  • You can't use simple assignment with strings. You need to use strcpy.

    strcpy(rooms[0][0].objects[0].name, "Cabinet");