c - word scramble with pointers in an array. Crashes when run -
i new arrays pointers, , trying make array of pointers word scramble game allows 3 tries guess word before game ends. basically, have created function scrambles string. then, string sent new string, shown user. user enters guess. getting no signal compiler on wrong.. crashes when run. believe error when sending pointer method. please tell me why error happening? thanks.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <ctype.h> void scramble(char *strings) { int length = strlen(strings), i, randomnum; char temp; for(i = 0; < length/2; i++) { randomnum = rand()%length; temp = strings[i]; strings[i] = strings[length - randomnum]; strings[length - randomnum] = temp; } } int main() { int i, tries, numwords; char *words[] = { "pumpkin", "cantalope", "watermelon", "apple", "kumquat" }; char *scramwords, *user; numwords = strlen(words); srand(time(null)); for(i = 0; < numwords; i++) { scramwords[i] = words[i]; scramble(scramwords[i]); } printf("how play: 3 tries guess each scrambled word.\n"); for(i = 0; < numwords; i++) { tries = 0; while(tries !=4) { if(tries == 3) { printf("you lose\n"); return 0; } printf("unscramble: %s\n", scramwords[i]); gets(user); if(strcmp(user, words[i]) == 0) { printf("correct!\n"); break; } else { tries++; } } } printf("you win!"); return 0; }
- you must not try modify string literals, or invoke undefined behavior. copy strings before editing them instead of assigning pointers.
length - randomnum
maylength
whenrandomnum
0.strlen(words)
won't number of elements inwords
. can usesizeof(words) / sizeof(*words)
.- you must allocate buffer
scramwords
,user
before writing there. - you shouldn't use
gets()
, has unavoidable risk of buffer overrun, deprecated in c99 , removed c11.
try this:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <ctype.h> void scramble(char *strings) { int length = strlen(strings), i, randomnum; char temp; for(i = 0; < length/2; i++) { randomnum = rand()%length; temp = strings[i]; strings[i] = strings[length - randomnum - 1]; strings[length - randomnum - 1] = temp; } } int main(void) { int i, tries, numwords; char *words[] = { "pumpkin", "cantalope", "watermelon", "apple", "kumquat" }; char **scramwords, user[1024], *lf; numwords = sizeof(words) / sizeof(*words); srand(time(null)); scramwords = malloc(sizeof(*scramwords) * numwords); if(scramwords == null) { perror("malloc"); return 1; } for(i = 0; < numwords; i++) { scramwords[i] = malloc(strlen(words[i]) + 1); /* +1 terminating null-character */ if(scramwords[i] == null) { perror("malloc"); return 1; } strcpy(scramwords[i], words[i]); scramble(scramwords[i]); } printf("how play: 3 tries guess each scrambled word.\n"); for(i = 0; < numwords; i++) { tries = 0; while(tries !=4) { if(tries == 3) { printf("you lose\n"); return 0; } printf("unscramble: %s\n", scramwords[i]); if(fgets(user, sizeof(user), stdin) == null) { puts("fgets failed"); return 1; } if((lf = strchr(user, '\n')) != null) { *lf = '\0'; /* remove newline character after string read */ } if(strcmp(user, words[i]) == 0) { printf("correct!\n"); break; } else { tries++; } } } printf("you win!"); return 0; }
Comments
Post a Comment