473,659 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing by Value

In Chapter 2 of the book 'C Unleashed' the author states that this
code

#include <stdio.h
void increment(char *p)
{
++p
}

int main(void)
{
char *s = "Hello world";
increment(s);
printf("%s\n", s);
return 0;
}

"doesn't work (and in fact results in undefined behavior):"

Modified code that does work, that is, code that prints "ello world"
is not shown.

What is is proper way to modify the code so that "ello world" is
printed?

Robert Wishlaw

Apr 5 '07 #1
11 1429
irwish...@gmail .com wrote:
In Chapter 2 of the book 'C Unleashed' the author states that
this code

#include <stdio.h
Did you mean #include <stdio.h ?
void increment(char *p)
{
++p
Did you mean ++p; ?
}

int main(void)
{
char *s = "Hello world";
increment(s);
printf("%s\n", s);
return 0;
}

"doesn't work (and in fact results in undefined behavior):"
Undefined behaviour? Was it originally ++*p; ?
Modified code that does work, that is, code that prints "ello
world" is not shown.

What is is proper way to modify the code so that "ello world" is
printed?
'Proper' is relative, but the following suffices...

void increment(char **p) /* pointer to a pointer */
{
++(*p); /* parentheses are actually redundant */
}

increment(&s);

--
Peter

Apr 5 '07 #2
ir*******@gmail .com writes:
In Chapter 2 of the book 'C Unleashed' the author states that this
code
[...syntacticall y incorrect code and incorrect conclusion...]
I never would have thought that Richard Heathfield would make so
many mistakes about basic stuff. Live and learn, I guess.
--
Ben Pfaff
http://benpfaff.org
Apr 5 '07 #3
On Apr 4, 7:30 pm, "Peter Nilsson" <a...@acay.com. auwrote:
irwish...@gmail .com wrote:
In Chapter 2 of the book 'C Unleashed' the author states that
this code
#include <stdio.h

Did you mean #include <stdio.h ?
Yes.
void increment(char *p)
{
++p

Did you mean ++p; ?
Yes.
}
int main(void)
{
char *s = "Hello world";
increment(s);
printf("%s\n", s);
return 0;
}
"doesn't work (and in fact results in undefined behavior):"

Undefined behaviour? Was it originally ++*p; ?
Modified code that does work, that is, code that prints "ello
world" is not shown.
What is is proper way to modify the code so that "ello world" is
printed?

'Proper' is relative, but the following suffices...

void increment(char **p) /* pointer to a pointer */
{
++(*p); /* parentheses are actually redundant */
}

increment(&s);
Thank you.
--
Peter

Apr 5 '07 #4
On Apr 4, 7:30 pm, "Peter Nilsson" <a...@acay.com. auwrote:
irwish...@gmail .com wrote:
In Chapter 2 of the book 'C Unleashed' the author states that
this code
#include <stdio.h

Did you mean #include <stdio.h ?
void increment(char *p)
{
++p

Did you mean ++p; ?
}
int main(void)
{
char *s = "Hello world";
increment(s);
printf("%s\n", s);
return 0;
}
"doesn't work (and in fact results in undefined behavior):"

Undefined behaviour? Was it originally ++*p; ?
No, the code in the book is ++p;.

Modified code that does work, that is, code that prints "ello
world" is not shown.
What is is proper way to modify the code so that "ello world" is
printed?

'Proper' is relative, but the following suffices...

void increment(char **p) /* pointer to a pointer */
{
++(*p); /* parentheses are actually redundant */
}

increment(&s);

--
Peter

Apr 5 '07 #5
ir*******@gmail .com said:
In Chapter 2 of the book 'C Unleashed' the author states that this
code

#include <stdio.h
void increment(char *p)
{
++p
}

int main(void)
{
char *s = "Hello world";
increment(s);
printf("%s\n", s);
return 0;
}

"doesn't work (and in fact results in undefined behavior):"
I state no such thing. What I actually say is:

(begin quote)

What is actually happening here is that the addresses [...] are being
passed - by value. We can see this most clearly with a simple
demonstration program:

#include <stdio.h>
void increment(char *p)
{
++p;
}

int main(void)
{
char *s = "Hello world";
increment(s);
printf("%s\n", s);
return 0;
}

If C passed pointers by reference, this program would print

ello world

But it doesn't. It prints

Hello world

This indicates that the modification of p by the increment() function
has no effect on the original pointer s. This is exactly the behavior
we would expect of pass-by-value. For precisely the same reason, this
code doesn't work (and in fact results in undefined behavior):

#include <stdio.h>

int openfile(char *s, FILE *fp)
{
fp = fopen(s, "r");
return fp ? 1 : 0;
}

int main(void)
{
FILE *fp;
char buffer[1024];
if(openfile("re adme", fp))
{
while(fgets(buf fer, sizeof buffer, fp))
printf("%s", buffer);
fclose(fp);
printf("\n");
}
return 0;
}

(end quote)

The original text, btw, spelled "behavior" as "behaviour" , but it got
mangled during production (apparently on purpose).
>
Modified code that does work, that is, code that prints "ello world"
is not shown.

What is is proper way to modify the code so that "ello world" is
printed?
This has already been answered elsethread.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 5 '07 #6
Ben Pfaff said:
ir*******@gmail .com writes:
>In Chapter 2 of the book 'C Unleashed' the author states that this
code
[...syntacticall y incorrect code and incorrect conclusion...]

I never would have thought that Richard Heathfield would make so
many mistakes about basic stuff. Live and learn, I guess.
To get the best out of this book, I strongly recommend that you read it.
:-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 5 '07 #7
Ben Pfaff wrote:
ir*******@gmail .com writes:
>In Chapter 2 of the book 'C Unleashed' the author states that this
code [...syntacticall y incorrect code and incorrect conclusion...]

I never would have thought that Richard Heathfield would make so
many mistakes about basic stuff. Live and learn, I guess.
Nicely snide, and slyly inserted. Now twist.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

--
Posted via a free Usenet account from http://www.teranews.com

Apr 5 '07 #8
ir*******@gmail .com wrote:
In Chapter 2 of the book 'C Unleashed' the author states that this
code
There are two many typographical errors for this to really be the code.
#include <stdio.h
void increment(char *p)
{
++p
}
int main(void)
{
char *s = "Hello world";
increment(s);
printf("%s\n", s);
return 0;
}
"doesn't work (and in fact results in undefined behavior):"
Modified code that does work, that is, code that prints "ello world"
is not shown.
What is is proper way to modify the code so that "ello world" is
printed?

#include <stdio.h /* mha: added '>' */

void increment(char **p /* mha: added '*' */ )
{
++*p; /* mha: added ';', added '*' */
}

int main(void)
{
char *s = "Hello world";
increment(&s); /* mha: added '&' */
printf("%s\n", s);
return 0;
}

Apr 5 '07 #9
Martin Ambuhl said:
ir*******@gmail .com wrote:
>In Chapter 2 of the book 'C Unleashed' the author states that this
code

There are two many typographical errors for this to really be the
code.
Presumably on the grounds that two is two many. :-) Incidentally, the
OP's other claim (that I had said the behaviour of the code is
undefined) is also incorrect. See my parallel reply.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3959
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
15
4667
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. However, since in C++ an array is passed by reference by default I need to embed the array into a struct in order to pass it by value. The problem is that I get a segmentation error when doing so. I'm using the Dev-c++ compiler. Any ideas? ...
58
10121
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
7
2852
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID parameter to the XSLT stylesheet XsltArgumentList xsltArgList = new XsltArgumentList(); xsltArgList.AddParam("pmID", "", pmID); xmlItems.TransformArgumentList = xsltArgList;
9
2295
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then receiving it, would that change the answer to the above?
6
5988
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local." My code is: using System; using System.Collections.Generic;
17
2804
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to free()? #include <stdlib.h> struct stype { int *foo; };
12
2677
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
12
5385
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
7
3297
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
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...
1
8525
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,...
0
8627
tracyyun
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...
0
5649
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();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.