C-分段故障与strlen

C-分段故障与strlen,c,pointers,segmentation-fault,C,Pointers,Segmentation Fault,我几个小时来一直在试图找出这个分割错误。每次我使用char*查找时,都会出现seg故障 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)

我几个小时来一直在试图找出这个分割错误。每次我使用char*查找时,都会出现seg故障

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;
当我运行该程序时,它会在运行时正确地划分故障

len = strlen(find) + 1
有人知道它为什么这样做吗?提前感谢。

scanf(“%s”,&find)应该是
scanf(“%s”,find)
find
已经是字符串的地址


而且
tolower
也不正确:
inttolower(intc)
int-tolower(字符*c)

另一种方法是使用
const char*
指向字符串文本,并将代码的其余部分更改为:

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);

因为您打算使用
scanf(“%s”,find)可能。
tolower()
不是这样工作的。禁用编译器警告或忽略它们。而且不可能
sizeof(char)!=1
所以不要用这个。哦,天哪。我这段时间都没用过墙。感谢您指出这一点。在
printf的
scanf
tolower
len
之间放置调试打印(“此处!\n”)tolower()
?我认为
char*find=(char*)malloc(sizeof(char)*20)是分配内存的正确方法。@ NulLoPosik:这个被高调投票的问题说,不要抛出MALOC的结果(C,C++是一种不同的语言):@戴夫:Syxx,你在哪里发现需要映射MalOC的结果?@环>指的是代码>空隙*Malc(siZeHT);<代码>我不应该这么做吗?好吧,用谷歌搜索一下。对于intance,@ringø:thanx对于这一点,导致为什么
char*find=malloc(sizeof(char)*20)
CLion是否会显示不兼容的指针类型“char*”和“void*”
…它不应该遵循这种做法吗?