Passing by Value

I

irwishlaw

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
 
P

Peter Nilsson

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

#include <stdio.h

Did you mean #include said:
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);
 
B

Ben Pfaff

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.
 
I

irwishlaw

Did you mean #include <stdio.h> ?

Yes.


Did you mean ++p; ?

Yes.



Undefined behaviour? Was it originally ++*p; ?



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

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

increment(&s);

Thank you.
 
R

Richard Heathfield

(e-mail address removed) 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.
 
R

Richard Heathfield

Ben Pfaff said:
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.
:)
 
C

CBFalconer

Ben said:
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
 
M

Martin Ambuhl

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;
}
 
R

Richard Heathfield

Martin Ambuhl said:
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

irwishlaw

Martin Ambuhl said:



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
 
R

Richard Heathfield

(e-mail address removed) 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top