2013-08-28 56 views
1

我正在尝试编写就地反转函数,并且几乎完全遵循在线代码,但运行以下程序会引发总线错误。我是否将错误类型的参数传递给reverse()?写入字符串时出现分段错误

void reverse(char *str) { 
    char * end = str; 
    char tmp; 
    if (str) { 
     while (*end) { 
      ++end; 
     } 
     --end; 
     while (str < end) { 
      tmp = *str; 
      *str++ = *end; 
      *end-- = tmp; 
     } 
    } 
} 

int main() { 
    char *s = "sample"; 
    reverse(s); 
    printf("%s\n"); 
    return 1; 
} 
+0

我相当肯定你的意思'的printf( “%S \ n”,S);'。 – cHao

+0

你应该写'const char * s =“sample”;'。得到它了?你知道为什么字符串常量被称为常量吗?没有?更好地谷歌它。 – 2013-12-26 10:58:50

回答

4

您可能需要更改

char *s = "sample"; //Pointer to string literal 

char s[] = "sample"; // mutable copy of string literal 

这是不确定的行为,试图修改字符串常量,它们应作为const char *


也许无关,而是一个建议,

当你这样做*end--,你可能想要把paranthesis,这样它做什么,你认为它。

上面可以

(*end)-- 

*(end--) 

而你需要的优先规则把握好,以确保你想要的是发生了什么。

+0

UB在C以及,所以这个答案是正确的,为什么崩溃。但是原始代码在优先级上是正确的;解引用运算符的优先级低于后缀增量/减量。 – verbose

+0

@verbose谢谢,我更熟悉C++,所以没有把握 –

3

您的反向功能是完全正确的。只需用主要功能部件几件事:

  • 为KarthikT说,S应该是char[]char*因为改变文字是不确定的。

  • 在您的printf函数中,您忘记将s作为参数。

  • return 0成功。 return 1是错误的。

所以新的主应该是这样的:

int main() { 
    char s[] = "sample"; 
    reverse(s); 
    printf("%s\n", s); 
    return 0; 
} 

输出:

elpmas 
14

要知道发生了什么事,你要明白一个C程序的内存布局。

char *s = "sample"; // Here the "sample" string is placed in 
         // the read only memory of the Initialized Data segment. 

在这里,您不能修改数据。 “s”是指向char const(“样本”)的指针,并且您正在尝试修改char const。这就是为什么你得到bus error错误。

     |Stack frame of main()   | 
         |char *s      | 
         |-------------------------------| 
         |Stack frame of reverse()  | 
         |char *end      | 
         |char tmp      | 
         |        | 
         |-------------------------------| 
         |        | 
         |        | 
         |        | 
         |        | 
         |        | 
         |-------------------------------| 
         |        | 
         |   HEAP    | 
         |        | 
         |-------------------------------| 
         |        | 
         | UNINITIALIZED DATA (BSS) | 
         |        | 
         |-------------------------------| 
         |        | 
         |  INITIALIZED DATA   | 
         |        | 
         |"sample" |     | 
         |   |     | 
         |(Read Only)| (Read/Write)  | 
         |-------------------------------| 
         | Text or Code Segment  | 
         |        | 
         |-------------------------------| 

UPDATE 后下不涉及您的问题。但是如果你知道在C中为所有变量分配的内存在哪里,那么你可以更好地编写代码。 下面的程序可以更好地理解C程序的内存布局。 我没有在图中包含函数的命令行参数,函数参数和返回值。 想要更新这篇文章的人可以将函数的命令行参数,函数参数和返回值添加到图中。

|Stack frame of main()    |    
|local_To_Main      | 
|         | #include <stdio.h> 
|-----------------------------------| #include <stdlib.h> 
|Stack frame of function1()   | int gVariable1 = 100; 
|local_To_Function1     | int gVariable2; 
|iptr        | char cstring[10] = "Hello"; 
|  \    STACK   | char* cptr = "Hello World"; 
|------\---------------|------------| void function1(void) 
|  \    \|/   | { 
|  \       |  static int j = 5; 
|   \       |  int local_To_Function1; 
|   \    ^ |  int *iptr; 
|   \    |  |  iptr = (int *) malloc(sizeof(int)); 
|------------\---------------|------|  free(iptr); 
| HEAP  \  ---   | } 
|    \---> |int|   | 
|      ---   | int main(void) 
|-----------------------------------| { 
|         |  static int i; 
| UNINITIALIZED DATA (BSS)  |  int local_To_Main; 
|gVariable2(initialized to 0)  | 
|i (initialized to 0)    | 
|-----------------------------------|  function1(); 
|         |  return 0; 
|  INITIALIZED DATA    | } 
|         | 
|"Hello World" |gVariable1 =100 | 
|  ^  |cstring="Hello" | 
|  |  |j=5    | 
|  |---<---<---- cptr   | 
|(Read Only) | (Read/Write)  | 
|-----------------------------------| 
| Text or Code Segment   | 
|         | 
|-----------------------------------| 
1
char *str="sample"; 

这里样品被存储在只读存储器,所以你永远不能让这样的文字修改。

为了进行操作,您必须将文字存储在读写内存中。 以下代码可以解决您的问题。

#include<stdio.h> 
void reverse(char *str) 
{ 
    char temp,*end; 
    for(end=str;*end;end++); 
    end--; 
    for(;str<end;temp=*str,*(str++)=*end,*(end--)=temp); 
} 

int main() 
{ 
    char str[]="sample"; //stored in read-write memory 
    reverse(str); 
    printf("%s\n",str); 
return 0; 
} 

输出:

elpmas 
相关问题