I've been trying to figure out this segmentation fault for hours. Every time I use the char* find I get a seg fault.
char* find = malloc(sizeof(char) * 20);
displayMatrix(rowcol, matrix);
// (Don't mind that ^^^)
printf("Enter a word to find in the puzzle : \n");
scanf("%s", &find);
tolower(find);
len = strlen(find) + 1;
When I run the program, it seg faults right as it gets to
len = strlen(find) + 1
Anyone know why it's doing this? Thanks in advance.
scanf("%s", &find);
should be scanf("%s", find);
find
is already the address of the string.
and tolower
is incorrect, too: int tolower ( int c );
not int tolower ( char *c );
Other way to do it would be point to a string literal using const char *
and changing the rest of the code as:
const char* find = (char*) malloc(sizeof(char)*20);
printf("Enter a word to find in the puzzle : \n");
scanf("%s", find);
printf("find says : I have got this saved under me %s \n", find); //just tried checking if it works
tolower(find); //this surely needs some correction,determine what you want
int len = strlen(find); //you won't require +1 using strlen
printf("%d : It Works Fine.",len);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.