value or refernce

R

rahulsinner

Hi,

K&R states C as strictly pass by value while a numerous other no. of
books suggest both pass by value and pass by reference.
So there are a hell lot of people out there who argue in favour of
existence of pass by reference.
Consider the following:

#include <stdio.h> #include
<stdio.h>
#include <stdlib.h> void
swap(int *,int *);

int
main(void)
{
void foo(int *);
int i=10,j=20;

swap(&i,&,j);
int main(void)
return 0;
{ }
int *i=NULL;
foo(i);
void swap(int *ptr1,int *ptr2)
return 0; {
}
int temp=*ptr1;

*ptr1=20;
void foo(int *ptr)
*ptr2=10;
{
return;
ptr=(int *)malloc(sizeof *ptr); }
return;
}

The one on the right is often cited as the proof for "pass by value"
and the code on the right is the classical example of pass by
reference.
So,what should be the one's a[[roach when one is asked whether C is
pass by reference or not?
And if it is not,can anyone tell me a language which is?
 
K

Keith Thompson

K&R states C as strictly pass by value while a numerous other no. of
books suggest both pass by value and pass by reference.
So there are a hell lot of people out there who argue in favour of
existence of pass by reference.

C passes arguments by value. You can achieve the effect of
pass-by-reference by passing a pointer; the pointer is, of course
passed by value. That's all.
 
P

P.J. Plauger

K&R states C as strictly pass by value while a numerous other no. of
books suggest both pass by value and pass by reference.

Then I suggest that those other books are wrong, or that you've
read them wrong.
So there are a hell lot of people out there who argue in favour of
existence of pass by reference.

Not to mention ghosts, effortless weight-loss programs, and peace
with honor.
Consider the following:

#include <stdio.h> #include
<stdio.h>
#include <stdlib.h> void
swap(int *,int *);

int
main(void)
{
void foo(int *);
int i=10,j=20;

swap(&i,&,j);
int main(void)
return 0;
{ }
int *i=NULL;
foo(i);
void swap(int *ptr1,int *ptr2)
return 0; {
}
int temp=*ptr1;

*ptr1=20;
void foo(int *ptr)
*ptr2=10;
{
return;
ptr=(int *)malloc(sizeof *ptr); }
return;
}

The one on the right is often cited as the proof for "pass by value"
and the code on the right is the classical example of pass by
reference.

I can't distinguish the one on the right from the one on the right;
your formatting got scrambled in the posting.
So,what should be the one's a[[roach when one is asked whether C is
pass by reference or not?

I simply say that it's not.
And if it is not,can anyone tell me a language which is?

Yes, C++ among many others. If you declare swap(int&, int&) then
the compiler secretly passes unmodifiable pointers to the function,
which are secretly dereferenced within the function, so you can
either read or write the arguments. In C, you pass explicit pointers
*by value*, and you explicitly dereference the pointers within the
function, to do the same thing.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
M

Martin Ambuhl

Hi,

K&R states C as strictly pass by value while a numerous other no. of
books suggest both pass by value and pass by reference.

Please name these "numerous" other books so we can warn people away from
them. If you have understood correctly what you read in them, then
their authors know nothing about C and are frauds and charlatans.
So there are a hell lot of people out there who argue in favour of
existence of pass by reference.

Name them. Either you misunderstand those people, or they are
discussing some other programming language, or they have their heads up
their asses. Anonymous "numerous" or "hell lot of" doesn't cut it. If
you want to make the logical fallacy of argument from authority, at
least name the authority you are fallaciously invoking.
Consider the following:

Consider learning how to format your code.
 
K

Keith Thompson

Martin Ambuhl said:
Please name these "numerous" other books so we can warn people away
from them. If you have understood correctly what you read in them,
then their authors know nothing about C and are frauds and charlatans.
[snip]

It's not uncommon to refer to C's common idiom of passing a pointer as
"pass by reference". We've had arguments here about whether that's
the right way to talk about it, but it's not *necessarily* incorrect.

But it's more accurate to say that passing a pointer is a way of
implementing the effect of pass-by-reference.
 

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,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top