Thread: Reverse a string

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    1

    Reverse a string

    I am trying to write a program to reverse a string passed from main() to another function, but am getting segmentation fault (most likely on line #32). Could you please point out my mistake?

    Thanks.

    Code:
      1 #include <stdio.h>
      2 #include <string.h>
      3 
      4 void reverse_string(char*, int start, int end);
      5 
      6 int main()
      7 {
      8   char *str = "This is a Test String";
      9 
     10   
     11 
     12   reverse_string(str, 0, strlen(str)-1);
     13 
     14   printf("str after reverse = %s\n",str);
     15 
     16   return 0;
     17 }
     18 
     19 void reverse_string(char* str, int start, int end)
     20 {
     21   int i,j;
     22   char temp_ch;
     23   printf("entering reverse_string\n");
     24   printf("str = %s\n",str);
     25 
     26   
     27 
     28   for(i=start,j=end; i<=end/2; i++,j--)
     29   {
     30     printf("i = %d\n",i);
     31     temp_ch = str[i];
     32     str[i] = str[j];
     33     str[j] = temp_ch;
     34   }
     35 
     36   return ;
     37 }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    When you say
    Code:
      8   char *str = "This is a Test String";
    you're declaring a pointer to a string literal, which itself resides in read-only memory. Good compilers will warn you about this if you enable warnings (e.g., "-W -Wall" with gcc). If you said
    Code:
      8   char str[] = "This is a Test String";
    instead, that would declare an array with just enough space to hold the string (including the NULL terminator), and the array would be initialized with that data. This is what you want.

    You see, you were trying to modify the read-only memory that the string literal is stored in: -- a sure way to get a segmentation fault.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM