difference b/w call by reference and call by pointer

R

ravi

Hi,

i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by
pointer (with example if possible).
 
K

Kenny McCormack

Hi,

i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by
pointer (with example if possible).

It is much like the difference between the Easter Bunny and George Bush.

One exists and the other doesn't (and more's the pity).

I.e., what you call "call by pointer" is the poor substitute for what
we'd really like (call by reference).
 
M

mark_bluemel

Hi,

i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by pointer (with example if possible).

It's probably more helpful to understand that C has only one calling
convention - call by value.

When a function in C is called, arguments are passed to it "by value"
- a simple approach (not strictly accurate, but workable in my
opinion) is to regard this as creating copies of the data passed, and
passing these. Changes made in the called function are not visible to
the calling function.

$ cat ravi.c
#include <stdlib.h>
#include <stdio.h>
void ravi(int a);
int main(void) {
int a = 10;
printf("in main before calling ravi - a is %d\n",a);
ravi(a);
printf("in main after calling ravi - a is %d\n",a);
return EXIT_SUCCESS;
}
void ravi(int a) {
printf("\tin ravi - a is initially %d\n",a);
a += 20;
printf("\tin ravi - a is now %d\n",a);
}

$ ./ravi
in main before calling ravi - a is 10
in ravi - a is initially 10
in ravi - a is now 30
in main after calling ravi - a is 10

If data in the calling function is to be modified by the called
function, then you must explicitly (except in one special case, which
I'll talk about in a moment) use pointers.

$ cat ravi2.c
#include <stdlib.h>
#include <stdio.h>
void ravi(int *a); /* function now takes a pointer to int */
int main(void) {
int a = 10;
printf("in main before calling ravi - a is %d\n",a);
ravi(&a); /* pass the address of a rather than its value */
printf("in main after calling ravi - a is %d\n",a);
return EXIT_SUCCESS;
}
void ravi(int *a) {
printf("\tin ravi - a is initially %d\n",*a);
*a += 20;
printf("\tin ravi - a is now %d\n",*a);
}

$ ./ravi2
in main before calling ravi - a is 10
in ravi - a is initially 10
in ravi - a is now 30
in main after calling ravi - a is 30

The exception (sort of) is when you pass an array as an argument to a
function.

In C, the arrayname's value is the address of the first element of the
array, so passing the array allows the called function direct access
to the array.
 
R

Richard Bos

ravi said:
i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by pointer (with example if possible).

There's no such thing as call by reference in C. There's no such thing
as call by pointer at all.

All parameters are passed by value in C. To pass a pointer in C, you
pass a pointer, TTBOMK just as you would in C++.

As usual (*sigh*) reading the FAQ before you posted here would have made
you a wiser man: <http://c-faq.com/ptrs/passbyref.html>.

Richard
 
L

lovloverboy

Hi,

i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by
pointer (with example if possible).
 
S

santosh

ravi said:
Hi,

i am a c++ programmer,

Judging by your lack of comprehension of basic concepts in the other
thread, you might want to change the word 'programmer' to 'student'.
now i want to learn programming in c also.

Right. So pickup a copy of "The C Programming Language" by Kernighan &
Ritchie and start doing the exercises.
so can anybody explain me the difference b/w call by reference and
call by pointer (with example if possible).

Why do you start a new thread for the same beaten topic? There's no
call by reference and no "call by pointer", whatever that is, in C. C
only supports call by value semantics. However by using pointers we
can, for most purposes, simulate what should be called as call by
reference. Note the word simulate, since that's what it is.
 
R

Richard Tobin

ravi said:
so can anybody explain me the difference b/w call by reference and
call by
pointer (with example if possible).

C provides only call by value. Passing a pointer is a way to
simulate call-by-reference, though you have to explicitly dereference
the pointer wherever you use it in the called function.

In a C-like call-by-reference language, you could do

void foo(int x)
{
x = x+1;
}

int main(void)
{
int a;
foo(a);
...
}

and the value of a would be changed. In real C, you would do:

void foo(int *x) /* the parameter type is a pointer to int */
{
*x = *x + 1; /* to access the int, you must dereference the pointer */
}

int main(void)
{
int a;
foo(&a); /* you pass a pointer to a rather than a itself */
...
}

[Array names are converted to pointers in almost all contexts so
arrays are effectively call-by-reference for most purposes.]

-- Richard
 
K

Keith Thompson

The exception (sort of) is when you pass an array as an argument to a
function.

In C, the arrayname's value is the address of the first element of the
array, so passing the array allows the called function direct access
to the array.

Strong emphasis on the "sort of".

In fact, you cannot pass an array as an argument to a function. An
expression of array type (such as the name of an array object) is
implicitly *converted*, in most contexts, to a pointer to the array's
first element. (The exceptions are when the array expression is the
operand of a unary "&" or "sizeof" operator, or is a string literal in
an initializer used to initialize an array object.)

This conversion really has nothing to do with function calls; an
argument in a function call is just one of the many contexts in which
the conversion takes place. You're not passing an array; you're
passing a pointer value (through which the actual array can be
accessed).

For more information, see section 6 of the comp.lang.c FAQ,
<http://www.c-faq.com>.
 
K

Keith Thompson

santosh said:
Why do you start a new thread for the same beaten topic? There's no
call by reference and no "call by pointer", whatever that is, in C. C
only supports call by value semantics. However by using pointers we
can, for most purposes, simulate what should be called as call by
reference. Note the word simulate, since that's what it is.

To say the same thing in another way, call-by-reference *as a language
feature* does not exist in C; it only has call-by-value. But
call-by-reference *as a programming technique* can easily be
implemented in C; it's implemented by passing a pointer value.
(Whether you call this "simulation" is a matter of taste, I suppose.)

It's similar to the question of whether C has linked lists. There's
no built-in language feature that directly implements linked lists,
but you can easily create linked lists using the lower-level building
blocks (pointers, structures, malloc, free) that the language
provides. I wouldn't necessarily refer to this as "simulating" linked
lists, unless I was thinking in terms of some other language that
provides linked lists as a built-in language feature.
 
J

Jack Klein

Hi,

i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by
pointer (with example if possible).

It's pretty simple in C, because there is no such thing as call by
reference.

All arguments to C functions are passed by value. If you want a C
function to be able to modify some object in the caller, you pass a
pointer to that object.

So there is no example of call by reference in C, and passing a
pointer looks just the same in C as it does in C++.
 
C

Chris Torek

can anybody explain me the difference b/w call by reference and ...

As others have already said, C does not have "by-reference"
parameters.

The real key to understanding this is that the various "by-*"
mechanisms, as found in different programming languages, reflect
the *usage* of parameter names in the *source code* in those
languages.

Suppose, for instance, we do this, in some unspecified language
whose syntax sort-of-resembles C:

procedure foo(int: ax, ay) {
ax <- ay;
ay <- 0;
}

procedure main() {
int: x, y;

x <- 40;
y <- 42;
foo(x, y);
print(x, y);
}

suppose we compile and run this and the output is:

42 0

We can infer from this that this language probably uses one of
three parameter-passing mechanisms: by-reference, by-name, or
"value-result". We can do this because, without any special syntax,
foo() was able to modify main()'s x and y (though they are called
ax and ay in foo()). There are four common parameter-passing
mechanisms: the three I just named, and "by-value". The last
one does not permit such modifications.

The way to tell by-reference from by-name and value-result is
to do something trickier. This time, we compile, e.g.:

int global <- 99; // global variable, initially 99

procedure foo(int: ax) {
print("before changing ax:", ax, global);
ax <- 42; // sets ax to 42, but does it affect "global"?
print("after changing ax:", ax, global);
}

procedure main() {
foo(global);
print("after calling foo:", global);
}

If we compile and run this, we might expect:

before changing ax: 99 99
after changing ax: 42 42
after calling foo: 42

This is what we will get from either by-reference or by-name. The
call in main() passes something that *refers to* the global variable
(either its name, which the system looks up somehow each time, or
a reference -- which in the end is just a pointer) and each use of
the parameter fetches or changes the actual global variable. Thus,
when foo() looks at the global variable using the name "global",
it sees the new value (42) just stored into it by its temporary
alias ("ax").

But if we see instead:

before changing ax: 99 99
after changing ax: 42 99
after calling foo: 42

then what we actually have is "value-result": main() passed the
*value* of the global variable to foo(); foo() modified a *copy*
(so the global variable remained 99); but then upon returning,
main() took the new, adjusted result back (so the global variable
became 42, to match the change to the parameter). The call passes
the value, then -- later -- takes the result, hence its name.

(Distinguishing by-name from by-reference is hardest. The canonical
method is to pass both i and a, and observe whether changes to
the passed-in "i" cause the other parameter to refer to the *new*
a. Since this is confusing, and call-by-name is quite rare,
I will omit this one entirely.)

Note that in a language like Pascal or C++, where reference parameters
*do* exist, no special syntax is required to *use* them (only to
declare them):

{ pascal }
procedure refparam(var x : integer);
begin
x := 42; { note: just plain "x" }
end;

// C++
void refparam(int& x) {
x = 42; // note: just plain "x"
}

But when you "fake" a reference parameter by passing a pointer
value by-value, as in C, you must then (a) "decorate" uses of that
pointer, and (b) also decorate the calls that will pass the pointer:

/* C */
void fakeref(int *x) {
*x = 42; /* note, "*x" not just "x" */
}

void func(void) {
int localvar;
...
fakeref(&localvar); /* note, "&localvar" not just "localvar" */
}

At its heart, the difference between "faking" by-reference using
pointer values in C, and true by-reference as found in other
languages, is just this syntactic decoration -- nothing less and
nothing more. But there are consequences to the "mere" syntax.
If we have to write "*x" in fakeref(), that gives us the opportunity
to write "x" without the "*":

void fakeref(int *x) {
*x = 42;
x = &othervar; /* changes x, not *x */
*x = 99; /* no longer changes func()'s "localvar" */
}

If we use the language's reference parameters (as in Pascal or
C++), we lose the opportunity to change "where x points". This
has both drawbacks and benefits: it is neither "good" nor "evil",
merely "different".
 

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
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top