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
7
  • 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...) Commented Nov 13, 2009 at 0:31
  • stackoverflow.com/questions/1614723/… Even the function name is the same. Commented Nov 13, 2009 at 0: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). Commented Nov 13, 2009 at 0:34
  • does this work for some inputs and not others? or does it just hang right away on the first input? Commented Nov 13, 2009 at 0:37
  • it hangs on the first input... Commented Nov 13, 2009 at 0:40

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Thank you @Pavel. Hope you don't mind that I copy your comment to my answer.
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).

1 Comment

getch() is in <conio.h>; it is of course non-standard (I think, originating from Turbo C).
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++;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.