C 总线错误:10。编译时没有错误
因此,基本上,当我使用GCC编译器编译代码时,我没有收到错误或警告,但当我输入第一段数据时,它会说:C 总线错误:10。编译时没有错误,c,macos,unix,bus-error,C,Macos,Unix,Bus Error,因此,基本上,当我使用GCC编译器编译代码时,我没有收到错误或警告,但当我输入第一段数据时,它会说: Bus error: 10. 我不确定我做错了什么。我认为问题来自void anagramgroupping(最后一个函数)。我还包括了代码的其余部分,以帮助遵循逻辑 #include <stdio.h> #include <string.h> #define Row 2 #define col 20 int wordCount = 0; int groupCoun
Bus error: 10.
我不确定我做错了什么。我认为问题来自void anagramgroupping
(最后一个函数)。我还包括了代码的其余部分,以帮助遵循逻辑
#include <stdio.h>
#include <string.h>
#define Row 2
#define col 20
int wordCount = 0;
int groupCount = 0;
char wordList[Row][col];
char group[Row][col];
// this is where prototypes go
void sortword(char word[col]);
void anagramGrouping(char word[col], char copy[col]);
void resetGroup();
int main() {
int i; // used in for loop to 'get' the strings
char word[col];
resetGroup();
for (i = 0; i < Row; i++) {
scanf("%s", word);
sortword(word);
wordCount++;
}
}
void resetGroup() {
int i;
for (i = 0; i < Row; i++)
strcpy(group[i], " ");
}
void sortword(char word[col]) {
int i = 0;
char temp;
char copy[col]; // used to store a copy of the original word
strcpy(copy, word);
while (word[i] != '\0') {
int j = i + 1;
while (word[j] != '\0') {
if (word[j] < word[i]) {
temp = word[i];
word[i] = word[j];
word[j] = temp;
}
j++;
}
i++;
}
anagramGrouping(word,copy);
}
void anagramGrouping(char word[col], char copy[col]) {
int n;
if (wordCount == 0) {
strcpy(group[0], copy);
}
for (n = 0; n <= groupCount; n++) {
if (strcmp(group[n], word) == 0) {
strcpy(group[n], copy);
} else {
groupCount++;
strcpy(group[groupCount], copy);
}
}
}
#包括
#包括
#定义第2行
#定义第20列
int字数=0;
int groupCount=0;
字符字表[行][col];
字符组[行][col];
//这就是原型的发展方向
void sortword(字符词[col]);
无效anagramGrouping(字符字[col],字符副本[col]);
void resetGroup();
int main(){
int i;//在for循环中用于“获取”字符串
字符字[col];
重置组();
对于(i=0;i
对于(n=0;n对于启动器,更改所有
sortword (char word[col])
到
诸如此类
anagramGrouping (char word[col], char copy[col])
到
传递char copy[col]
时,传递的是字符数组
,当作为函数参数传递时,该数组将转换为指针。(您将听到短语“衰减为指针”,这并不完全正确,但通常用于表示相同的意思)。这不是导致总线错误的问题,但会使代码更具可读性
其次,您的总线错误
通常是由于误用指针值造成的,该指针值在取值时超出了程序允许的内存范围,通常位于导致总线错误
的系统区域。查看您的代码,很难判断许多问题中的哪一个可能是确切原因。但是,可能是罪魁祸首是:
for(n=0; n <= groupCount; n++)
{
if ( strcmp(group[n],word) ==0 )
{
strcpy(group[n],copy);
}
else
{
groupCount++;
strcpy(group[groupCount],copy);
}
}
进一步清理代码可能如下所示:
#include<stdio.h>
#include<string.h>
#define Row 2
#define col 20
int wordCount = 0;
int groupCount = 0;
char wordList[Row][col];
char group[Row][col];
void sortword (char *word);
void anagramGrouping (char *word, char *copy);
void resetGroup();
int main (void) {
int i;
char word[col] = "";
resetGroup();
for (i = 0; i < Row; i++)
{
scanf ("%s", word);
sortword (word);
wordCount++;
}
return 0; /* main is a function of type 'int' and returns a value */
}
void resetGroup()
{
int i;
for (i = 0; i < Row; i++)
*group[i] = 0;
}
void sortword (char *word)
{
int i = 0;
char temp;
char copy[col] = "";
strcpy (copy, word);
while (word[i]) {
int j = i + 1;
while (word[j]) {
if(word[j] < word[i]) {
temp = word[i];
word[i] = word[j];
word[j] = temp;
}
j++;
}
i++;
}
anagramGrouping (word,copy);
}
void anagramGrouping (char *word, char *copy)
{
int n;
if (!wordCount)
strcpy (group[0],copy);
for (n = 0; n < Row; n++) {
if (strcmp (group[n], word) == 0)
strcpy(group[n],copy);
else {
groupCount++;
strcpy (group [groupCount],copy);
}
}
}
#包括
#包括
#定义第2行
#定义第20列
int字数=0;
int groupCount=0;
字符字表[行][col];
字符组[行][col];
void sortword(字符*单词);
无效anagramGrouping(字符*单词,字符*副本);
void resetGroup();
内部主(空){
int i;
字符字[col]=“”;
重置组();
对于(i=0;i|
如果您有任何问题,请告诉我。在valgrind或gdb中运行您的程序,查看哪一行触发了崩溃。很抱歉,没有更多帮助,请尝试检查您的指针/取消引用。您的代码显示得非常糟糕。请修复缩进并删除无用的//函数结束
注释。我已清理了我的代码。此外,我以前从未听说过valgrind或gdb。您的anagramGrouping
函数似乎就是问题所在。每次发现不匹配时,您都会增加groupCount
。在本例中,它不允许超过2(甚至不能达到2),但它几乎肯定会这样做,这将导致内存错误。关于不传递数组的所有废话都与问题无关,也是不好的建议。char-word[col]
比char*word
可读性和类型安全性更高。如果有任何问题,该函数应该更改为sortword(size\u-col,char-word)[col])
,使其独立于宏,甚至更安全。另外,关于您关于衰减是不正确的术语的评论……转换更不正确,因为标准使用的术语转换是在运行时发生的。数组衰减不是。C标准实际上使用了“调整”一词为了描述这一点(6.7.6.3):“将参数声明为“类型数组”应调整为“类型的限定指针”,其中类型限定符(如有)是在数组类型派生的[和]中指定的。”这一点很好。'adjust'
就是这样——完成后,您将得到一个转换为指针的数组。:)
@Lundin我不同意您关于是否在参数列表中显示数组或指针的评论。由于数组是“调整的”
,函数不接收数组,它接收指向数组的指针,从而使指针明确指示函数可用的内容。数组的优点是yle是因为它支持更高的类型安全性,所以好的编译器或
for(n=0; n <= groupCount; n++)
{
if ( strcmp(group[n],word) ==0 )
{
strcpy(group[n],copy);
}
else
{
groupCount++;
strcpy(group[groupCount],copy);
}
}
for (n = 0; n < Row; n++) {
#include<stdio.h>
#include<string.h>
#define Row 2
#define col 20
int wordCount = 0;
int groupCount = 0;
char wordList[Row][col];
char group[Row][col];
void sortword (char *word);
void anagramGrouping (char *word, char *copy);
void resetGroup();
int main (void) {
int i;
char word[col] = "";
resetGroup();
for (i = 0; i < Row; i++)
{
scanf ("%s", word);
sortword (word);
wordCount++;
}
return 0; /* main is a function of type 'int' and returns a value */
}
void resetGroup()
{
int i;
for (i = 0; i < Row; i++)
*group[i] = 0;
}
void sortword (char *word)
{
int i = 0;
char temp;
char copy[col] = "";
strcpy (copy, word);
while (word[i]) {
int j = i + 1;
while (word[j]) {
if(word[j] < word[i]) {
temp = word[i];
word[i] = word[j];
word[j] = temp;
}
j++;
}
i++;
}
anagramGrouping (word,copy);
}
void anagramGrouping (char *word, char *copy)
{
int n;
if (!wordCount)
strcpy (group[0],copy);
for (n = 0; n < Row; n++) {
if (strcmp (group[n], word) == 0)
strcpy(group[n],copy);
else {
groupCount++;
strcpy (group [groupCount],copy);
}
}
}