【问题标题】:segmentation fault occur while reversing the string反转字符串时发生分段错误
【发布时间】:2015-06-16 23:10:13
【问题描述】:

下面是我的一段代码,我不明白为什么它总是给我分段错误:

#include <stdio.h>
void reverse(void);

int main ()
{
    printf("enter the text");
    printf("\n");
    reverse(); 

    printf("\n");
    return(0);
} 
void reverse(void)
{
    char c;
    if((c=getchar()) != '\n')
    {
        reverse();
    }
    putchar(c);
}

在我看来,我所做的一切都是正确的,有什么错误?

【问题讨论】:

  • 对我来说运行良好。你能告诉我们它在哪条线路上为你造成了故障吗?
  • “立即帮助!”不是我们喜欢在这里听到的话。
  • 也许你可以帮助我们告诉你的操作系统。
  • @Jaspreet Singh 详细说明失败的输入。
  • 它似乎可以正常工作,但您不是在“反转字符串”,而是在反向打印输入。

标签: c segmentation-fault


【解决方案1】:

只要您输入换行符,代码就可以正常工作。也许您正在使用 EOF(通常绑定到 Ctrl+D)来终止您的输入,而之前没有为其提供换行符,在这种情况下,代码将永远不会看到换行符,并且由于无限递归而将出现堆栈溢出。

因此,您应该检查getchar() 不会返回EOF。此外,getchar() 返回 int,而不是 char - 这对于可移植性和确保与 EOF 的比较按预期工作很重要。

解决这些问题后的代码如下:

#include <stdio.h>

void reverse(void);

int main (void) {
    printf("enter the text\n");
    reverse(); 
    printf("\n");
    return 0;
}

void reverse(void) {
    int c;
    if ((c=getchar()) != '\n' && c != EOF) {
        reverse();
    }
    if (c != EOF) {
        putchar(c);
    }
}

【讨论】:

    【解决方案2】:

    您的程序在我的设置上编译并运行良好:Ubuntu 14.04.2 LTS 64 位上的最新稳定 gcc。

    这是使用不同方法的另一个版本(即fgets 函数)。看看它是否适合你:

    #include <stdio.h>
    #include <string.h>
    
    void reverse_str( char * );
    
    int main()
    {
        char input[1024];
    
            printf("Enter text: ");
            fgets(input, sizeof(input), stdin);
    
            reverse_str(input);
    
            printf("Reversed string: %s\n", input);
    
        return 0;
    }
    
    void reverse_str(char *to_reverse)
    {
        char temp[1024];
        int count = strlen(to_reverse) - 1; //Exclude newline introduced with fgets
        int i=0;
    
            for( i=count; i>=0; i-- ){
                temp[i] = to_reverse[count - i - 1]; //Subtract 1 to not include the new line introduced by fgets
            }
    
            temp[count+1] = '\0';
            strcpy(to_reverse, temp);
    }
    

    【讨论】:

    • 编辑:正如@molbdnilo 在 cmets 中指出的那样,从技术上讲,您的代码不会反转字符串。另一方面,我编写的函数就是这样做的,因此它适用于通过 stdin 以及其他更通用的应用程序的输入,只要字符串的长度小于或等于临时字符串长度(您可以更改) .
    【解决方案3】:

    由于 getchar() 的讨厌字符,您的代码似乎失败了。在大多数系统中它应该可以工作,但我认为您的编译器正在尝试访问保存在数组之外的内存并因此产生分段错误...你能确定你是否用'\0'代替'\n',它是否正常工作..我认为问题是你的机器无法检测到键盘给出的'\n'&因此继续进入递归模式&在递归结束之前堆栈溢出&当堆栈溢出时,它试图访问未经授权的内存&因此发生分段错误

    试试这个

    #include <stdio.h>
    #include <string.h>
    char str[] = "Hello World";
    size_t length;
    int count = 0;
    
    void reverse(char* a, char* b){
    //  static int count = 0;
      char temp;
      if (count < length/2){
        count++;
        reverse(str + count, str + (length - 1) - count);
      }
        temp = *a;
        *a = *b;
        *b = temp;
    }
    int main(){
    length = strlen(str);
    reverse(str, str + length - 1);
    printf("%s", str);
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 2011-03-11
      • 2023-03-31
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 2012-11-14
      • 1970-01-01
      相关资源
      最近更新 更多