2013-08-28 61 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 
相關問題