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 1797
* 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.c om/c++-faq-lite/newbie.html#faq-29.3>
<url: http://www.research.at t.com/~bs/bs_faq2.html#vo id-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?
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.
>>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<<end l;
a =0;
b = 10;
a = b++;
cout<<a<<b<<end l;
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jordan Rastrick |
last post by:
Can anybody please give me a decent justification for this:
class A(object):
def __init__(self, a):
self.a = a
def __eq__(self, other):
return self.a == other.a
s = A(3)
|
by: Mark Stijnman |
last post by:
A while ago I posted a question about how to get operator behave
differently for reading and writing. I basically wanted to make a
vector that can be queried about whether it is modified recently or
not. My first idea, using the const and non-const versions of
operator, was clearly not correct, as was pointed out. Julián Albo
suggested I could use proxies to do that. I've done some googling for
proxies (also in this group) and personally,...
|
by: jane |
last post by:
HI,
I had a question on incremental backup.
We had an incremental backup every weekend. We did full backup every
other week. That is one week incremental + full , the other week is
incremental only. My question is for the first weekend incremental
backup, it took for example 1h, but the second weekend incremental
took almost 3 hours. It seems always this way, so I suppose it is not
related to the data change. And we almost had some...
|
by: Zri Man |
last post by:
I have come across a bizzare behaviour with DB2/UDB 8.2 on SuSE Linux
2.41
When I have a MQT Refresh going on (complete refresh) it appears to
lock the underlying base tables used to build the MQT.
When I attempt to SELECT these tables in other sessions, it simply
refuses to yield and are waiting for the MQT to finish.
Any clues ? Is this the intended behaviour?
|
by: Willem |
last post by:
Based on MK's TSI_SOON (http://www.trigeminal.com/)I've created a
nifty little procedure that - whenever you compact you db you get an
incremental backup copy.
Given that you have a table with version information you get
incremental backups on a per-version basis.
SEE CODE BELOW
Basic idea is: start TSISOON with the options:
1. "compact this db"
| |
by: James J. Foster |
last post by:
Is there a way to instruct visual studio to give each instance of a user
control a unique attribute value, by specifying a pattern for an incremental
index? Similar to how visual studio automatically determines a unique ID
value.
For example, if my control has an attribute panelnumber, as in:
<my:panel panelnumber="pan1" runat="server" id="panel1" />
I want to establish a pattern placeholder, eg "pan##", whereby VS.NET will
|
by: Frankie |
last post by:
I understand that .NET Framework 3.5 is currently in Beta-2, with initial
TRM expected sometime near the end of this year, with the big-time launch in
February.
Question: Are there major changes to ASP.NET that we can expect? Or would
this be considered as an "incremental" update.
I'm asking because I'm about to launch a major refactoring effort on a
large/complex ASP.NET 1.1 Web application... just wondering if I should go
with 2.0...
|
by: Claire |
last post by:
Hi,
I can imagine this question has been brought up before but I've spent all
morning trying to find what I need on google without success.
My application sits on Mysql or MS sql server engines and to date I've just
been using auto-incremental int64 fields to generate my RecID field.
I know that in the future the database will need to support multiple sites,
so theres also a SiteID field in there too.
example table ABC
Primary Key =...
|
by: aaanishaaa |
last post by:
hi all....
i want to know how to count the unique number of operators. i have used the following (c# code)....
StreamReader sr = new StreamReader(fname);
Regex operator_pat=new Regex(@"");
MatchCollection mc,mc1;
string code=sr.ReadToEnd();
mc=operator_pat.Matches(code);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |