sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Bangalore's Avatar

Unique behaviour of incremental operators in recursion


Question posted by: Bangalore (Guest) on July 26th, 2005 07:55 AM
Hi
In reversing string usring recursive function, i found problems in
using incremental postfix , and incremental prefix


1 void rev(char*);
2 void main()
3 {
4 char s[]="STRING";
5 rev(s);
6 }
7 void rev(char* s)
8 {
9 if(*s != '\0')
10 {
11 rev(s++);
12 printf("%c",*s);
13 }
14 else
15 return;
16 }

If run the above code, excution will be stuck in infinite loop(It will
be in first address of the string).
if I chage postfix increment operator (line 11) to prefix increment
operator.
In the output it won't print first character.
If I replace s++ with s+1 ( line 11 ), I am getting correct output.
Plz clarify me, why postfix and prefix incremental operators are
behaving
like that,
also explain me how the function call is being done with postfx
incremental
operatorpostfx incremental
operator.

Thanks

5 Answers Posted
Ram's Avatar
Guest - n/a Posts
#2: Re: Unique behaviour of incremental operators in recursion

See <url: http://www.parashift.com/c++-f aq-lite/how-to-post.html#faq-5
..2>.

Ramashish

Alf P. Steinbach's Avatar
Alf P. Steinbach July 26th, 2005 08:25 AM
Guest - n/a Posts
#3: Re: Unique behaviour of incremental operators in recursion

* Bangalore:[color=blue]
> In reversing string usring recursive function, i found problems in
> using incremental postfix , and incremental prefix
>
>
> 1 void rev(char*);
> 2 void main()[/color]

Incorrect.
<url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3>
<url: http://www.research.att.com/~bs/bs_faq2.html#void-main>


[color=blue]
> 3 {
> 4 char s[]="STRING";
> 5 rev(s);
> 6 }
> 7 void rev(char* s)
> 8 {
> 9 if(*s != '\0')
> 10 {
> 11 rev(s++);[/color]

<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_02_11.html>

[color=blue]
> 12 printf("%c",*s);[/color]

As a newbie, use C++ iostreams.

[color=blue]
> 13 }
> 14 else
> 15 return;[/color]

Indentation.
[color=blue]
> 16 }
>
> If run the above code, excution will be stuck in infinite loop(It will
> be in first address of the string).[/color]

See above, or look up the definition of postfix ++ in your textbook.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
kk's Avatar
Guest - n/a Posts
#4: Re: Unique behaviour of incremental operators in recursion

You cant change the address of array variable s, bcoz STRING directly
stores in variable s (it's done with the statement s[]="STRING").
i think it may possible through char *s="STRING". in this case s
contains the address (memory location of string i.e "STRING" )

prefix and postfix operators cant be applied on array variables.

try it.

upashu2's Avatar
Guest - n/a Posts
#5: Re: Unique behaviour of incremental operators in recursion

>>Bangalore wrote: excution will be stuck in infinite loop
See the code.[color=blue][color=green]
>> rev(s++);
>> printf("%c",*s);[/color][/color]
You are using postfix operator , so First it will call rev(s) , and
then it will increment s by one. So each time it is l calling only
rev(s) recursively infinite time.[color=blue][color=green]
>> if I chage postfix increment operator (line 11) to prefix increment
>>operator.
>>In the output it won't print first character.[/color][/color]
if u do[color=blue][color=green]
>> rev(++s);
>> printf("%c",*s);[/color][/color]
then it is already incrementing s, so you will next character printed.
Just , print first and call rev() later, you will get what u want.
[color=blue][color=green]
>> printf("%c",*s);
>> rev(++s);[/color][/color]
[color=blue][color=green]
>> If I replace s++ with s+1 ( line 11 ), I am getting correct output.[/color][/color]
No doubt, since s +1 means simply add 1 to s and then pass to rev()
function. It doesn't change the value of s itself.[color=blue][color=green]
>> Plz clarify me, .[/color][/color]
Use a good book on C/C++.
writing ++s, means
s = s+1
and then use s.
writing s++ means
first use s and then s = s+1.
test this
int a=0, b =10;
a = ++b;
cout<<a<<b<<endl;
a =0;
b = 10;
a = b++;
cout<<a<<b<<endl;

Greg's Avatar
Guest - n/a Posts
#6: Re: Unique behaviour of incremental operators in recursion

upashu2 wrote:[color=blue][color=green][color=darkred]
> >>Bangalore wrote: excution will be stuck in infinite loop[/color][/color]
> See the code.[color=green][color=darkred]
> >> rev(s++);
> >> printf("%c",*s);[/color][/color]
> You are using postfix operator , so First it will call rev(s) , and
> then it will increment s by one. So each time it is l calling only
> rev(s) recursively infinite time.[/color]


No, consider this line more closely:

rev(s++);

what actually happens is this: s is incremented *before* the call to
rev, not afterwards. And s is not actually passed to rev (because it
has already been incremented). Now it may appear that the compiler is
in a bit of bind. What does it pass to rev? Fortunately the compiler
had the foresight to make a copy of s before it was incremented, so it
passes this copy of s in the function call to rev.

In C++, all expressions, even those with postincrement operations,
which appear as parameters in a function call must be completely
evaluated before the function call can be made.

Greg

 
Not the answer you were looking for? Post your question . . .
196,814 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,814 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors