1

したがって、基本的に GCC コンパイラでコードをコンパイルすると、エラーや警告は表示されませんが、最初のデータを入力すると、次のように表示されます。

Bus error: 10.

何が間違っているのかわかりません。void anagramGrouping問題は(最後の関数)から来ていると思います。ロジックに従うのに役立つ残りのコードも含めました。

#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);
        }
    }
}
4

1 に答える 1

-1

まず、すべてを変更します

sortword (char word[col])

sortword (char *word)

そしてすべて

anagramGrouping (char word[col], char copy[col]) 

anagramGrouping (char *word, char *copy)

を渡すときはchar copy[col]、を渡します。character arrayこれは、関数パラメーターとして渡されると、ポインターに変換されます。( 「decays to a pointer」というフレーズが聞こえますが、これは完全に正しいわけではありませんが、同じ意味でよく使用されます)。これは を引き起こす問題ではありませんがbus error、コードがより読みやすくなります。

次に、bus error通常、値が取得されたときに、プログラムの許容メモリ範囲外になるポインター値の誤用が原因であり、通常はシステム領域でbus error. コードを見てみると、多くの問題のどれが正確な原因であるかを判断するのは困難です。ただし、考えられる原因は次のとおりです。

for(n=0; n <= groupCount; n++)
{
    if ( strcmp(group[n],word) ==0 )
    {
        strcpy(group[n],copy);
    }
    else
    {
        groupCount++;
        strcpy(group[groupCount],copy);
    }
}

デバッガーを実行するgroupCountと、. 許容される行の値は、あなたのように制限されています。1strcpy(group[groupCount],copy)char group[Row][col];0-1#define Row 2

コピーを許容範囲に制限するには、ループ基準を次のように変更します。

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

ご不明な点がございましたら、お知らせください。

于 2016-03-14T04:08:42.050 に答える