difference b/w call by reference and call by pointer

Discussion in 'C Programming' started by ravi, Feb 22, 2007.

  1. ravi

    ravi Guest

    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).
     
    ravi, Feb 22, 2007
    #1
    1. Advertising

  2. In article <>,
    ravi <> 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).
     
    Kenny McCormack, Feb 22, 2007
    #2
    1. Advertising

  3. ravi

    Guest

    On 22 Feb, 12:09, "ravi" <> 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.
     
    , Feb 22, 2007
    #3
  4. ravi

    Richard Bos Guest

    "ravi" <> 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
     
    Richard Bos, Feb 22, 2007
    #4
  5. ravi

    Guest

    On Feb 22, 2:09 pm, "ravi" <> 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).
     
    , Feb 22, 2007
    #5
  6. ravi

    santosh Guest

    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.
     
    santosh, Feb 22, 2007
    #6
  7. In article <>,
    ravi <> 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.
     
    Richard Tobin, Feb 22, 2007
    #7
  8. 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) <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.
     
    Keith Thompson, Feb 22, 2007
    #8
  9. "santosh" <> 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) <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.
     
    Keith Thompson, Feb 22, 2007
    #9
  10. ravi

    Jack Klein Guest

    On 22 Feb 2007 04:09:09 -0800, "ravi" <> 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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Feb 23, 2007
    #10
  11. ravi

    Chris Torek Guest

    In article <>
    ravi <> wrote:
    >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".
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.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.
     
    Chris Torek, Feb 23, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vols
    Replies:
    14
    Views:
    601
    Howard
    Aug 1, 2006
  2. Replies:
    4
    Views:
    407
    Andre Kostur
    Dec 24, 2007
  3. thomas
    Replies:
    8
    Views:
    530
    James Kanze
    Jul 13, 2008
  4. A
    Replies:
    7
    Views:
    649
  5. somenath
    Replies:
    10
    Views:
    302
    James Kanze
    Jul 2, 2013
Loading...

Share This Page