0

An array of pointers to strings is provided as the input. The task is to reverse each string stored in the input array of pointers. I've made a function called reverseString() which reverses the string passed to it. This functions works correctly as far as i know.

The strings stored/referenced in the input array of pointers are sent one by one to the reverseString() function. But the code hangs at some point in the reverseString() function when the values of the passed string are swapped using a temp variable. I can't figure out why the code is hanging while swapping values. Please help me with this.

The code is as follows:

#include <stdio.h>
void reverseString(char*);

int main()
{   char *s[] = {"abcde", "12345", "65gb"};
    int i=0;
    for(i=0; i< (sizeof(s)/sizeof(s[0]) ); i++ )
    {   reverseString(s[i]);
        printf("\n%s\n", s[i]);
    }

    getch();
    return 0;
}//end main

void reverseString(char *x)
{   int len = strlen(x)-1;
    int i=0; 
    char temp;
    while(i <= len-i)
    {   temp = x[i];
        x[i] = x[len-i];
        x[len-i] = temp;
            i++;
    }
}//end reverseString
nhylated
  • 1,575
  • 13
  • 19
  • i'm learning C programming on my own from a reference book. this question is one of the exercises in the book. so i guess this cannot be categorized as homework, can it? (i'm not very clear about this since i'm a newbie at stackoverflow...) – nhylated Nov 13 '09 at 00:31
  • http://stackoverflow.com/questions/1614723/why-is-this-c-code-causing-a-segmentation-fault Even the function name is the same. – AnT stands with Russia Nov 13 '09 at 00:33
  • Be very sure your while loop isn't doing integer overflow and wrapping around. The loop could run for a long long time if that happens (and appear to hang). – quark Nov 13 '09 at 00:34
  • does this work for some inputs and not others? or does it just hang right away on the first input? – JustJeff Nov 13 '09 at 00:37
  • it hangs on the first input... – nhylated Nov 13 '09 at 00:40
  • how does it hang? does it just go away and not come back? or does it abort saying something like 'segmentation fault'? – JustJeff Nov 13 '09 at 00:42
  • i'm using Bloodshed Dev-C++ for coding on Windows. No error statement like 'segmentation fault' is displayed. the code just hangs and the output screen remains blank. although the problem is solved now. pmg's solution worked. – nhylated Nov 13 '09 at 00:55

3 Answers3

6

You are trying to change string literals.

String literals are usually not modifiable, and really should be declared as const.

const char *s[] = {"abcde", "12345", "65gb"};
/* pointers to string literals */

If you want to make an array of modifiable strings, try this:

char s[][24] = {"abcde", "12345", "65gb"};
/* non-readonly array initialized from string literals */

The compiler will automatically determine you need 3 strings, but it can't determine how long each needs to be. I've made them 24 bytes long.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • To clarify: in the first example (and code in the question), you have pointers to string literals (which are read-only). In the second example, you have a non-readonly array initialized _from_ string literals. – Pavel Minaev Nov 13 '09 at 00:42
  • Thank you @Pavel. Hope you don't mind that I copy your comment to my answer. – pmg Nov 13 '09 at 00:50
2

The strings ("abcde" etc) could be stored in readonly memory. Anything is possible when you try to modify those strings, therefore. The pointers to the strings are modifiable; it is just the strings themselves that are not.

You should include <string.h> to obtain the declaration of strlen(3), and another header to obtain the function getch() - it is not in <stdio.h> on my MacOS X system (so I deleted the call; it is probably declared in either <stdio.h> or <conio.h> on Windows).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

Hope this helps you! what i am doing here is that i am going to the address of the last character in the string then printing them all by decreasing the pointer by 1 unit (for character its 2 bytes(please check)).

//program to reverse the strings in an array of pointers
#include<stdio.h>
#include<string.h>
int main()
{
    char *str[] = {
        "to err is human....",
        "But to really mess things up...",
        "One needs to know C!!"
    };
    int i=0;    //for different strings
    char *p;    //declaring a pointer whose value i will be setting to the last character in 
                //the respective string
    while(i<3)  
    {
        p=str[i]+strlen(str[i])-1;
        while(*p!='\0')
        {
            printf("%c",*p);
            p--;
        }
        printf("\n");       
        i++;
    }
}
Andreas
  • 5,393
  • 9
  • 44
  • 53