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

void rec(char pin[]);

main()
{
      char pin[100];
      printf("Give word: ");
      scanf("%s", pin);
      rec(pin);
      system("pause");
}

void rec(char pin[])
{
     int i=0;
     if (pin[i]=='\0')
        return;

     else
     {    
        rec(pin[i+1]);
        printf("%c", pin[i]);

     }

}

Well seems not to work but I don't know why. (I am not allowed to use the for loop, the function strlen and things like that).

3
  • 1
    you call the rec() function but it's declaration is : re(....). Typo ? Commented May 17, 2013 at 7:47
  • A more thorough description of the behavior, rather than 'seems not to work' would be helpful. Can you post some output? Commented May 17, 2013 at 7:50
  • 1
    rec(pin[i+1]); error at this line initializing argument 1 of 'void rec(char*)' Commented May 17, 2013 at 7:52

4 Answers 4

3

in rec function else part you are passing a element which should be address of element.so try this in else part

else
     {    
        rec(&pin[i+1]);
        printf("%c", pin[i]);

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

1 Comment

At this situation, the program starts, but crushes after the scanf.
1

Well, since your question is "why it doesn't work", might as well answer exactly that.

I'm going to assume that the re() declaration is just a typo for rec() -- of course you have to correct that.

In the first line of that function, you declare a variable, int i = 0;. However, that variable is never assigned to again. Scan the function for any assignment on i -- you won't find any. Therefore, that i variable is a constant 0. With that in mind, let's replace i by 0 and write the code again:

if (pin[0]=='\0')
    return;
else
{    
   rec(pin[1]);
   printf("%c", pin[0]);
}

The offending line is clearly rec(pin[1]). The function expects a char * argument, i.e., a string (note that char * and char [] are the same in function parameter declarations). However, pin[1] is just the second character of pin. What you're doing there is converting implicitly that character to a pointer and passing it to the function -- which is incorrect.

What you want to pass to rec() is the pointer to the second character, since that would make it a pointer to a string beginning at the second character of pin. So, the correct call would be rec(pin + 1), not rec(pin[1]). Since pin points to the first character of the string, pin + 1 points to the second.

Comments

1

This is not correct. First of all, you are using an automatic variable. so, 'i' will always be initialized to 0.

use static int i, and see carefully. you are throwing char to char*. so, you cannot throw rec(pin[i+1]); change this to rec(pin); and before printf("%c", pin[i]); decrement 'i', before calling rec recursively, increment 'i' . Last but not least, you are calling 'rec'. but function name is 're', where is c???

Comments

0
void rec(char pin[]){
    if (*pin=='\0')
        return;
    else {
        rec(pin + 1);
        printf("%c", *pin);
    }
}

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.