By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,331 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

difference b/w call by reference and call by pointer

P: n/a
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).

Feb 22 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
In article <11**********************@j27g2000cwj.googlegroups .com>,
ravi <dc**********@gmail.comwrote:
>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).

Feb 22 '07 #2

P: n/a
On 22 Feb, 12:09, "ravi" <dceravigu...@gmail.comwrote:
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.

Feb 22 '07 #3

P: n/a
"ravi" <dc**********@gmail.comwrote:
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
Feb 22 '07 #4

P: n/a
On Feb 22, 2:09 pm, "ravi" <dceravigu...@gmail.comwrote:
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).

Feb 22 '07 #5

P: n/a
ravi wrote:
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.

Feb 22 '07 #6

P: n/a
In article <11**********************@j27g2000cwj.googlegroups .com>,
ravi <dc**********@gmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 22 '07 #7

P: n/a
ma**********@pobox.com writes:
[...]
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>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 22 '07 #8

P: n/a
"santosh" <sa*********@gmail.comwrites:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 22 '07 #9

P: n/a
On 22 Feb 2007 04:09:09 -0800, "ravi" <dc**********@gmail.comwrote
in comp.lang.c:
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++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 23 '07 #10

P: n/a
In article <11**********************@j27g2000cwj.googlegroups .com>
ravi <dc**********@gmail.comwrote:
>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[i], and observe whether changes to
the passed-in "i" cause the other parameter to refer to the *new*
a[i]. 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".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 23 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.