Use the '=' operator (not memcpy()) to shallow-copy structs.

This commit is contained in:
Jason S. Ninneman 2017-08-03 12:41:35 -07:00
parent 87c6d3fb1e
commit 26bf324e07
2 changed files with 7 additions and 13 deletions

16
main.c
View file

@ -1051,8 +1051,7 @@ Lclearobj:
game.knfloc = 0; game.knfloc = 0;
/* Preserve state from last command for reuse when required */ /* Preserve state from last command for reuse when required */
command_t preserve; command_t preserve = command;
memcpy(&preserve, &command, sizeof(command_t));
// Get command input from user // Get command input from user
if (!get_command_input(&command)) if (!get_command_input(&command))
@ -1123,8 +1122,7 @@ Lclearobj:
} }
if ((command.word[0].id == WATER || command.word[0].id == OIL) && (command.word[1].id == PLANT || command.word[1].id == DOOR)) { if ((command.word[0].id == WATER || command.word[0].id == OIL) && (command.word[1].id == PLANT || command.word[1].id == DOOR)) {
if (AT(command.word[1].id)) { if (AT(command.word[1].id)) {
memcpy(&command.word[1], &command.word[0], command.word[1] = command.word[0];
sizeof(command_word_t));
command.word[0].id = POUR; command.word[0].id = POUR;
command.word[0].type = ACTION; command.word[0].type = ACTION;
strncpy(command.word[0].raw, "pour", LINESIZE - 1); strncpy(command.word[0].raw, "pour", LINESIZE - 1);
@ -1137,13 +1135,9 @@ Lclearobj:
/* From OV to VO form */ /* From OV to VO form */
if (command.word[0].type==OBJECT && command.word[1].type==ACTION) { if (command.word[0].type==OBJECT && command.word[1].type==ACTION) {
command_word_t stage; command_word_t stage = command.word[0];
memcpy(&stage, &command.word[0], command.word[0] = command.word[1];
sizeof(command_word_t)); command.word[1] = stage;
memcpy(&command.word[0], &command.word[1],
sizeof(command_word_t));
memcpy(&command.word[1], &stage,
sizeof(command_word_t));
} }
} }

View file

@ -36,7 +36,7 @@ int savefile(FILE *fp, long version)
save.mode = -1; save.mode = -1;
save.version = (version == 0) ? VRSION : version; save.version = (version == 0) ? VRSION : version;
memcpy(&save.game, &game, sizeof(struct game_t)); save.game = game;
IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp)); IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
return (0); return (0);
} }
@ -119,7 +119,7 @@ int restore(FILE* fp)
if (save.version != VRSION) { if (save.version != VRSION) {
rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10)); rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
} else { } else {
memcpy(&game, &save.game, sizeof(struct game_t)); game = save.game;
} }
return GO_TOP; return GO_TOP;
} }