Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 交换函数中的分段错误_C++ - Fatal编程技术网

C++ 交换函数中的分段错误

C++ 交换函数中的分段错误,c++,C++,我正忙着为我的学习写一行代码。 我在这项任务上已经走得很远了,但我一直遇到同样的问题。 在swap函数中,当输入的字符(word&word2)不在主“dictionary”字符串中时,我总是遇到分段错误。 有人能给我解释一下是什么导致了这个问题以及我如何解决它吗?对不起,如果什么都不清楚,我刚开始学习C++。 发生分段错误的代码: void swapWords(char **dict, char *word, char *word2) { int i; int d;

我正忙着为我的学习写一行代码。 我在这项任务上已经走得很远了,但我一直遇到同样的问题。 在swap函数中,当输入的字符(word&word2)不在主“dictionary”字符串中时,我总是遇到分段错误。 有人能给我解释一下是什么导致了这个问题以及我如何解决它吗?对不起,如果什么都不清楚,我刚开始学习C++。 发生分段错误的代码:

  void swapWords(char **dict, char *word, char *word2)
{
    int i; 
    int d;
    int x;
    int y;
    char *tmp;


    while (1){  
        for(i = 0; i < MAX_NUMBER_OF_WORDS; i++)
        { 
            if(strcmp(word, dict[i]) != 0)
            {    
                if(i == MAX_NUMBER_OF_WORDS -1)
                {
                    printf("Cannot swap words. Atleast one word missing in the dictionary.\n");
                    goto error;
                }
            }
            else
            {
                x = i;
                break;  
            }

        }
        for(d = 0; d < MAX_NUMBER_OF_WORDS; d++)
        { 
            if(strcmp(word2, dict[d]) != 0)
            {    
                if(d == MAX_NUMBER_OF_WORDS -1)
                {
                    printf("Cannot swap words. Atleast one word missing in the dictionary.\n");
                    goto error;
                }
            }
            else
            {
                y = d;
                break;
            }

        }

        tmp = dict[x];
        dict[x] = dict[y];
        dict[y] = tmp;
        error: break;
    }
}
void swapWords(char**dict,char*word,char*word2)
{
int i;
int d;
int x;
int-y;
char*tmp;
而第(1)款{
对于(i=0;i
整个代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

#define MAX_NUMBER_OF_WORDS 10

void swapWords(char **dict, char *word, char *word2)
{
    int i; 
    int d;
    int x;
    int y;
    char *tmp;


    while (1){  
        for(i = 0; i < MAX_NUMBER_OF_WORDS; i++)
        { 
            if(strcmp(word, dict[i]) != 0)
            {    
                if(i == MAX_NUMBER_OF_WORDS -1)
                {
                    printf("Cannot swap words. Atleast one word missing in the dictionary.\n");
                    goto error;
                }
            }
            else
            {
                x = i;
                break;  
            }

        }
        for(d = 0; d < MAX_NUMBER_OF_WORDS; d++)
        { 
            if(strcmp(word2, dict[d]) != 0)
            {    
                if(d == MAX_NUMBER_OF_WORDS -1)
                {
                    printf("Cannot swap words. Atleast one word missing in the dictionary.\n");
                    goto error;
                }
            }
            else
            {
                y = d;
                break;
            }

        }

        tmp = dict[x];
        dict[x] = dict[y];
        dict[y] = tmp;
        error: break;
    }
}

void removeWord(char **dict, char *word)
{
    int i;
    int d;
    for(i = 0; i < MAX_NUMBER_OF_WORDS; i++)
    { 
        if(strcmp(dict[i], word) == 0)
        {   dict[i] = NULL;
            for(d = i+1; d < MAX_NUMBER_OF_WORDS; d++)
            { if(dict[d] == NULL)
                { dict[i] = dict[d-1];
                    dict[d-1] = NULL;
                    break;
                }

            }
            break;
        }
    }
}

void printDict(char **dict)
{
    int i = 0;

    if(dict[0] == NULL)
    {
        printf("The dictionary is empty.\n");
    }
    else{
    while (dict[i] != NULL)
    {
        printf("- %s\n", dict[i]);
        i++;
    }
    }
}

void addWord(char **dict, char *word)
{
    int d;
    char *word1;

            for(d = 0; d < MAX_NUMBER_OF_WORDS; d++)
            {
                if (dict[d] == NULL)
                {           
                word1 = (char*) malloc(sizeof(char)*(strlen(word) + 1));
                strcpy(word1, word);
                dict[d] = word1;

                break;
                }
            }
}
int numberOfWordsInDict(char **dict)
{
    int i = 0;
    int d;

    for (d = 0; d < MAX_NUMBER_OF_WORDS; d++){
    if(dict[d] != NULL)
    {
        i++;
    }
}

    return i;
}

int main()
{
    char *dict[MAX_NUMBER_OF_WORDS] = {};
    char word[36];
    char word2[36];
    char c;
    int i;
    while(printf("Command (a/p/r/s/q): "))
    {
    scanf(" %c", &c);
    switch (c){
    case 'p':   printDict(dict);
                break;
    case 'a':   printf("Enter a word: ");
                scanf("%s", word);
                addWord(dict, word);
                break;
    case 'n':   i = numberOfWordsInDict(dict);
                printf("%d\n", i);
                break;
    case 'r':   printf("Remove a word: ");
                scanf("%s", word);
                removeWord(dict, word);
                break;
    case 's':   printf("Swap two words:\n");
                printf("Enter first word: ");
                scanf("%s", word);
                printf("Enter second word: ");
                scanf("%s", word2);
                swapWords(dict, word, word2);
                break;
    case 'q':   return 0;
            }
        }
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#包括
#包括
#包括
#定义单词的最大数量10
无效交换字(字符**dict,字符*字,字符*字2)
{
int i;
int d;
int x;
int-y;
char*tmp;
而第(1)款{
对于(i=0;i
分段故障很可能发生在以下位置:

if(strcmp(word, dict[i]) != 0)
事实上,很可能i>大于dict的大小,如果dict有3个元素,而您试图访问第4个元素,则访问的是未知区域或ram,这会导致分段错误。
解决方案是确保for循环在字典的最后一个元素处停止,并使用解决方案πάνταῥεῖ 已在上述评论中提出

分段错误很可能发生在以下位置:

if(strcmp(word, dict[i]) != 0)
事实上,很可能i>大于dict的大小,如果dict有3个元素,而您试图访问第4个元素,则访问的是未知区域或ram,这会导致分段错误。 解决方案是确保for循环在