我是 C 的初学者,我正在编写一个程序来查找字符串中字符的频率。我在这里遇到访问冲突,但我无法解决。它在 while 循环中,我正在循环以获取在字符串数组中找到的每个唯一字符并将其存储在另一个字符中。我可以在这里帮忙吗?提前致谢。
int main()
{
char str[5][10];
int i=0, j=0, k, l=0;
char chh[10];
strcpy(chh, "a"); while (l < 5)
{
if (strcmp(chh[i],str[l][j]) != 0) <--- access violation in this line.
{
chh[i] == str[l][j];
i++;
}
if (j >= 9)
j = 0;
l++;
j++;
}
}
最佳答案
您正在使用 strcmp 比较两个字符。
http://www.cplusplus.com/reference/cstring/strcmp/
int strcmp ( const char * str1, const char * str2 );
你应该只删除那个 strcmp 并直接比较两个字符
chh[i] == str[l][j]
已编辑
你可能想要
if (j >= 9){
j = 0;
l++;
}
关于c - 0xC000005 读取时访问冲突,在 C 中使用 strcmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22131770/