473,503 Members | 1,731 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 1412
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
[...syntactically 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("readme", fp))
{
while(fgets(buffer, 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
[...syntactically 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 [...syntactically 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.securityfocus.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
On Apr 4, 10:13 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Martin Ambuhl said:
irwish...@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.
I apologize for my mistake. Upon rereading the paragraph I realize
that the last "this" in the paragraph refered to the following code
snippet and not the previous code snippet as I assumed and implied in
my original post.

I also apologize for the typographical errors in my post which could
have been avoided by simple cut and paste if the your statement on
page 15 that "The CD contains ... the complete text of the book
itself" was true. In fact the only code snippet on the CD from chapter
2 is obscure.c.

My main point in my original post was to find out how the example
cited could be rewritten to print "ello world". Peter Nilsson
graciously answered that question for which I am grateful.

Robert Wishlaw
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

Apr 5 '07 #11
ir*******@gmail.com said:
I apologize for my mistake. Upon rereading the paragraph I realize
that the last "this" in the paragraph refered to the following code
snippet and not the previous code snippet as I assumed and implied in
my original post.
And I apologise in turn for originally writing it in such a way that it
could be misinterpreted.

--
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 #12

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

Similar topics

8
3942
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
15
4649
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. ...
58
10046
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...
7
2828
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...
9
2288
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...
6
5977
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...
17
2789
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...
12
2662
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
5367
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
7
3290
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...
0
7087
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...
0
7281
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,...
1
6993
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...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
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...
0
4675
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...
0
1514
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 ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
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...

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.