Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > difference b/w call by reference and call by pointer

Reply
Thread Tools

difference b/w call by reference and call by pointer

 
 
ravi
Guest
Posts: n/a
 
      02-22-2007
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).

 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      02-22-2007
In article <(E-Mail Removed) .com>,
ravi <(E-Mail Removed)> wrote:
>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).

 
Reply With Quote
 
 
 
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      02-22-2007
On 22 Feb, 12:09, "ravi" <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      02-22-2007
"ravi" <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
lovloverboy@gmail.com
Guest
Posts: n/a
 
      02-22-2007
On Feb 22, 2:09 pm, "ravi" <(E-Mail Removed)> wrote:
> 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).



 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-22-2007
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.

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      02-22-2007
In article <(E-Mail Removed) .com>,
ravi <(E-Mail Removed)> wrote:

>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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-22-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-22-2007
"santosh" <(E-Mail Removed)> writes:
[...]
> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      02-23-2007
On 22 Feb 2007 04:09:09 -0800, "ravi" <(E-Mail Removed)> wrote
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
difference between pointer and reference thomas C++ 8 07-13-2008 06:30 PM
reference and pointer-reference kasthurirangan.balaji@gmail.com C++ 4 12-24-2007 09:05 PM
old question: difference between pointer and reference Vols C++ 14 08-01-2006 06:07 PM



Advertisments