strcpy()只将一个字符串复制到结构中,而不是另一个字符串

时间:2016-11-11 02:53:12

标签: c arrays string struct strcpy

我将其简化为基础,因为其余的代码是不必要的。这是一个基本的文本冒险计划。

Date

无论出于何种原因,输出都是:

// defines numeric values for each room
typedef enum {
    KITCHEN,
    PANTRY,
    HALLWAY_1,
    HALLWAY_2,
    TROLLS_DOMAIN,
    EMPTY_ROOM_1,
    EMPTY_ROOM_2,
    EXIT,
    NO_ROOM
} en_rooms;

// defines a struct type to store room data
typedef struct room{
    char name[50];
    char desc[50]; // description
} Room;

// fills the array r with info on each room.
void room_setup(Room *r){
    strcpy(r[KITCHEN].name, "Kitchen");
    strcpy(r[KITCHEN].desc, "This is the kitchen.");
}

int main(void){
    ...

    Room rooms[10];
    room_setup(rooms); // fill rooms array with info
    en_rooms currentRoom = KITCHEN; // set the starting room to the kitchen

    // main game loop
    while(1){

        ...

        // print room name + description
        printf("-- %s --\n", rooms[currentRoom].name);
        printf("%s\n", rooms[currentRoom].desc);

        ...
    }
}

任何人都可以帮我解决为什么strcpy()会复制房间描述,但不能正确地命名吗?

0 个答案:

没有答案