By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,118 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,118 IT Pros & Developers. It's quick & easy.

Unique behaviour of incremental operators in recursion

P: n/a
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

Jul 26 '05 #1
Share this Question
Share on Google+
5 Replies


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

Ramashish

Jul 26 '05 #2

P: n/a
* Bangalore:
In reversing string usring recursive function, i found problems in
using incremental postfix , and incremental prefix
1 void rev(char*);
2 void main()
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>
3 {
4 char s[]="STRING";
5 rev(s);
6 }
7 void rev(char* s)
8 {
9 if(*s != '\0')
10 {
11 rev(s++);
<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_02_11.html>

12 printf("%c",*s);
As a newbie, use C++ iostreams.

13 }
14 else
15 return;
Indentation.
16 }

If run the above code, excution will be stuck in infinite loop(It will
be in first address of the string).


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?
Jul 26 '05 #3

P: n/a
kk
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.

Jul 26 '05 #4

P: n/a
>>Bangalore wrote: excution will be stuck in infinite loop
See the code.
rev(s++);
printf("%c",*s); 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. if I chage postfix increment operator (line 11) to prefix increment
operator.
In the output it won't print first character. if u do rev(++s);
printf("%c",*s); 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.
printf("%c",*s);
rev(++s); If I replace s++ with s+1 ( line 11 ), I am getting correct output. 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. Plz clarify me, .

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;

Jul 26 '05 #5

P: n/a
upashu2 wrote:
Bangalore wrote: excution will be stuck in infinite loop See the code. rev(s++);
printf("%c",*s);

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.

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

Jul 27 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.