2

I am trying to implement a code that uses a function called reverse that takes a string and returns a new string that is the reverse of the old string. so far as I can tell the function itself works properly - i.e. I tested to see if the the reversed string: new string had the correct values and it did. However when I try to print this reversed string after it has been returned to the main function I get a bad access code. Does anyone have any ideas? Here is my code:

#include <stdio.h>
#include <string.h>

char reverse( char *string)   {
char newstring[50]; //reversed string to be returned to the main function
int size = strlen(string);
int index = 0;
for (index = 0 ; index < size ; index++)   {
    newstring[index] = *(string+size-index-1); //creates a new string that is the reverse of     the old string
}
return newstring; //reurns the new string
}


int main(int argc, const char * argv[]) {

char string[50];
printf("Please enter a string you wish to reverse:\n");
scanf("%49s", string); // user enters a string of maximum length 50
int size = strlen(string);
char *reverse_string;
reverse_string = reverse(string); // sets a pointer called reverse_string to the pointer returned by the reverse function
for ( int index = 0 ; index < size ; index++)   {
    printf("%c", *(reverse_string+index));// prints the reversed string value by value
}
    return 0;
}
3
  • 2
    Function can return the address of the string, but not the string itself. Commented Dec 5, 2014 at 4:05
  • 3
    returning newstring is a problem as newstring is a local variable. Commented Dec 5, 2014 at 4:06
  • Functions can return pointers. Commented Dec 5, 2014 at 4:26

5 Answers 5

2

You need to change the declaration of your function so that it returns char* not just a char. In you're main loop you are currently assigning a char to reverse_string not a char*.

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

2 Comments

ok so how would I do that? I tried changing the function declaration to: char* reverse( char *string) but when I tried reversing the string: apple all I got was e???0 as a result
You will also need to malloc (allocate memory for) the variable newstring within the reverse function as it is a local variable. Remember you can use the sizeof() function here to make things more robust :)
0

Whenever you open braces in c, a new stack frame is created and it is destroyed when you close the the corresponding braces. The problem is all the variables that were created inside these braces are also destroyed and any address you return is useless as it doesn't exist. The workaround for this problem is that you can allocate memory on the heap (using malloc) and return the address (heap is shared by the whole program) or you can pass string as an argument and use that to store the reversed string (pseudo call-by-reference).

Comments

0

Assuming the declaration of reverse() really has it returning char*, the problem is that you are returning a pointer to a variable (newstring) that was declared locally in reverse(). That means the variable's address is no longer accessible once you exit that function. You have at least these options:

  1. Declare the variable globally (above the function definition).

  2. Create the variable on the heap (e.g. use malloc):

    char *newstring = (char*) malloc(50);

Comments

0
char reverse( char *string)

Function returning is a char data type. It will allow you to return a character not a whole string. You can make the data type as returning pointer to char. char *. It will allow you to return a string.

Then make the variable using in that function and allocate the memory with malloc function. If you use the normal variable then it will not work.

Comments

0
  1. Code should not attempt to return the address of local variable newstring to be use for later referencing (printf("%c",...) as by that time, the local variable contents and existence are not valid.

  2. When returning a pointer, the function's return type should be a pointer like char *reverse() rather than char reverse().

  3. This all can be solved by reversing the string in place.

    void reverse2(char *string) {
      size_t rightp1 = strlen(string);
      size_t left = 0;
      while (left < rightp1) {
        rightp1--; 
        char t = string[left];
        string[left] =  string[rightp1];
        string[rightp1] = t;
        left++;
      }
    
    int main(int argc, const char * argv[]) {
       ...   
       scanf("%49s", string);
       reverse(string);
       fputs(string, stdout);
       return 0;
    }
    

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.