-3

Why it's not functioning? compiler says its returning local address. any help how to make it it correct. i saw other reverse string programs but i didn't find any problem in it.

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

char *reverse(char *str);

void main()
{
    char str[]="jamesbond";
    int len=strlen(str);
    char *ptr;
    ptr=reverse(str);
    printf("the reversed string is :%s",ptr);
}

char *reverse(char *str)
{ 
    int i,j;
    int len=strlen(str);
    char qtr[len];

    for( i=len-1;i<=0;i--)
    { 
        for( j=0;j<len-1;j++)
            qtr[j]=*(str+i);
    }
    return qtr;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Kundan Negi
  • 41
  • 1
  • 1
  • 6
  • 1
    You cant return `qtr`, it is not valid after `reverse` exits. – imreal Jan 24 '13 at 17:58
  • 1
    Please do a minimum of research before asking a question. Searching for the error message in Google will yield a ton of helpful results. – netcoder Jan 24 '13 at 18:00
  • Also, check [this](http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1043284376&answer=1044841143) please. – Grieverheart Jan 24 '13 at 18:06
  • http://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c – qulinxao Jan 24 '13 at 18:12
  • In short, you are returning garbage. Since you have pointers in C why not make reverse prototype like int reverse(char *instr, char *reversed) – Andro Jan 24 '13 at 18:17
  • Note that this question is merely a repost of Kundan's original post here: http://stackoverflow.com/questions/14504993/program-to-reverse-a-string – phonetagger Jan 24 '13 at 18:45

5 Answers5

0

returning a stack variable(or array) (here qtr) from a function is undefined behavior. It will be overwritten.

Solution is to malloc() the array and return that instead, then free() it when you're done using it.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

qtr is a local array, and you're returning a pointer to it. You can't do that. You need to allocate dynamic memory and return that:

char *qtr = malloc(len + 1);

Note that you will need to free() this memory sometime. Alternately, just do the reversal in place:

char *start = str;
char *end = str + strlen(str) - 1;
char tmp;

while (start < end)
{
   tmp = *start;
   *start++ = *end;
   *end-- = tmp;
}
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

qtr is being allocated on the stack. Memory which goes out of scope as soon as your function returns. You could declare qtr as static as a workaround.

itsme86
  • 19,266
  • 4
  • 41
  • 57
0

The condition for the first for loop must be change to

i >= 0



for( i=len-1;i>=0;i--)
  { for( j=0;j<len-1;j++)
    qtr[j]=*(str+i);

   }

And since you are not passing the pointer to the resulting array.

It is better to malloc for qtr

qtr = malloc(sizeof(strlen(str));

Also

 int len=strlen(str);

in your main() is an unwanted computation.

sr01853
  • 6,043
  • 1
  • 19
  • 39
0

I fount this pice of code somewhere. Looks legit. And if you don't want to ruinn you input string just deep copy the pointer (memcpy()) and insert it in.

char* strrev( char* s)
  {
  char  c;
  char* s0 = s - 1;
  char* s1 = s;

  /* Find the end of the string */
  while (*s1) ++s1;

  /* Reverse it */
  while (s1-- > ++s0)
    {
    c   = *s0;
    *s0 = *s1;
    *s1 =  c;
    }

  return s;
  }
Andro
  • 2,232
  • 1
  • 27
  • 40