Problem when i pass pointers to functions

Discussion in 'C Programming' started by ramif, Nov 11, 2007.

  1. ramif

    ramif Guest

    Does call by reference principle apply to pointers??
    Is there a way to pass pointers (by reference) to functions?

    Here is my code:


    #include <stdio.h>
    #include <stdlib.h>



    // f() is supposed to change num's value to 88...
    void f(int *num)
    {
    int *num2 = malloc( sizeof(int) );
    *num2 = 88;

    //*num = 88; //data OUTSIDE f() is changed
    num = num2; //data OUTSIDE function f() will not change...

    free(num2);
    }



    main()
    {
    int *x = malloc( sizeof(int));
    *x = 5;

    printf("x = %d\nExecuting f()...\n", *x);

    f(x); //calling f() to change the value of x
    printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

    free(x);
    }
    ramif, Nov 11, 2007
    #1
    1. Advertising

  2. ramif

    santosh Guest

    In article <fh79ea$s76$>, ramif
    <> wrote on Sunday 11 Nov 2007 9:42 pm:

    > Does call by reference principle apply to pointers??


    Strictly speaking there is no call by reference in C. It can however be
    simulated with pointer values.

    > Is there a way to pass pointers (by reference) to functions?


    Yes. Pass pointer to them.

    int *p;
    /* ... */
    fx(&p);
    /* ... */

    > Here is my code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > // f() is supposed to change num's value to 88...
    > void f(int *num)
    > {
    > int *num2 = malloc( sizeof(int) );
    > *num2 = 88;
    >
    > //*num = 88; //data OUTSIDE f() is changed
    > num = num2; //data OUTSIDE function f() will not change...
    >
    > free(num2);


    A return would be better form here.

    > }
    >
    > main()


    Implicit int is not legal anymore. Just declare main to return an int
    and place a 'void' between the parenthesis to indicate that it takes no
    arguments.

    > {
    > int *x = malloc( sizeof(int));
    > *x = 5;
    >
    > printf("x = %d\nExecuting f()...\n", *x);
    >
    > f(x); //calling f() to change the value of x
    > printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE
    >
    > free(x);


    And another return here.

    > }


    What exactly do you want to do? Change the value held at the location
    pointed to by 'x' or change what 'x' points to. To do the former f()
    can just say:

    *x = new_value;

    To do the latter you need to enable f() to act on main's 'x' not just
    it's local copy; pass the address of 'x' to do this:

    f(&x);

    /* in f() */
    *x = new_address;
    /* ... */
    santosh, Nov 11, 2007
    #2
    1. Advertising

  3. ramif

    Chris Dollin Guest

    ramif wrote:

    > Does call by reference principle apply to pointers??


    Yes. C doesn't have call by reference for anything, including
    pointers. (The nearest thing it has is that arrays decay into
    pointers in value context -- this can look like call by
    reference, but it isn't, not least because it's nothing to
    do with /calling/.)

    > Is there a way to pass pointers (by reference) to functions?


    No.

    What you /can/ do is to pass a /pointer-to-pointer-to/.

    > // f() is supposed to change num's value to 88...
    > void f(int *num)


    void f( int **num )

    > {
    > int *num2 = malloc( sizeof(int) );
    > *num2 = 88;
    >
    > //*num = 88; //data OUTSIDE f() is changed


    **num = 88;

    > num = num2; //data OUTSIDE function f() will not change...


    *num = num2;

    > free(num2);


    Hell's teeth, man, if you want the assignment to outside ths
    function to mean anything, /don't free `num2`/.

    > }
    >
    > main()
    > {
    > int *x = malloc( sizeof(int));
    > *x = 5;
    >
    > printf("x = %d\nExecuting f()...\n", *x);
    >
    > f(x); //calling f() to change the value of x


    f( &x );

    > printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE


    It will now. (Of course, you've thrown away your only pointer
    to the originally mallocated store. Oops.)

    > free(x);
    > }


    --
    Heapy Hedgehog
    Meaning precedes definition.
    Chris Dollin, Nov 11, 2007
    #3
  4. ramif

    Richard Guest

    ramif <> writes:

    > Does call by reference principle apply to pointers??
    > Is there a way to pass pointers (by reference) to functions?


    Someone will be along to say there is no call by reference. This is kind
    of true. But misleading and confuses nearly everyone. You can pass a
    pointer to an object which for all intents and meaning is a "reference"
    for the greater majority of programmers. So for me, passing a pointer is
    indeed pass by reference. No matter what the standard says : word games
    dont get the point across.

    >
    > Here is my code:
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    >
    >
    > // f() is supposed to change num's value to 88...
    > void f(int *num)
    > {
    > int *num2 = malloc( sizeof(int) );
    > *num2 = 88;
    >
    > //*num = 88; //data OUTSIDE f() is changed
    > num = num2; //data OUTSIDE function f() will not change...
    >
    > free(num2);
    > }
    >
    >
    >
    > main()
    > {
    > int *x = malloc( sizeof(int));
    > *x = 5;
    >
    > printf("x = %d\nExecuting f()...\n", *x);
    >
    > f(x); //calling f() to change the value of x
    > printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE


    Not an error : you didn't change the contents of what the pointer num
    points to in function f().

    You probably want to pass a pointer to the pointer x.

    f(&x);

    And in f(int **num)

    *num=newPointer;

    But of course you will need another copy of x in order to call free.

    Rather than repeat the same stuff over and over or rely on my accuracy
    in non compilable examples, have a look here:

    http://www.eskimo.com/~scs/cclass/int/sx8.html


    >
    > free(x);
    > }
    Richard, Nov 11, 2007
    #4
  5. ramif

    Chris Dollin Guest

    Richard wrote:

    > ramif <> writes:
    >
    >> Does call by reference principle apply to pointers??
    >> Is there a way to pass pointers (by reference) to functions?

    >
    > Someone will be along to say there is no call by reference.


    s/will/was/.

    > This is kind of true.


    As in, true up to arguments about definitions.

    > But misleading and confuses nearly everyone.


    It confuses people who have had it explained to them by confused
    people.

    > You can pass a pointer to an object which for all


    Most.

    > intents and meaning is a "reference" for the greater majority
    > of programmers.


    Indeed.

    > So for me, passing a pointer is indeed pass by reference.


    And here are the definitional things. No, passing a pointer
    isn't pass (or call) by reference. You can do all the things
    that pass by reference does by passing pointers, /except/
    the doing-it-implicitly that's part of the definition of
    pass-by-reference.

    > No matter what the standard says : word games
    > dont get the point across.


    Exactly. Your confusion of terms doesn't make C have pass-
    by-reference, in which the construction and dereference of
    the necessary pointers is implicit and part of the machinery
    of the language. C happens to have those constructions and
    dereferences as directly accessible features, so it /doesn't
    need/ to have pass-by-reference.

    In a language like (the original) Pascal, you couldn't
    simulate pass-by-reference with the rest of the language
    features, and so it was much clearer that pass-by-reference
    was a specific feature.

    --
    Historical Hedgehog
    The "good old days" used to be much better.
    Chris Dollin, Nov 11, 2007
    #5
  6. ramif

    Joe Wright Guest

    ramif wrote:
    > Does call by reference principle apply to pointers??
    > Is there a way to pass pointers (by reference) to functions?
    >
    > Here is my code:
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    >
    >
    > // f() is supposed to change num's value to 88...
    > void f(int *num)
    > {
    > int *num2 = malloc( sizeof(int) );
    > *num2 = 88;
    >
    > //*num = 88; //data OUTSIDE f() is changed
    > num = num2; //data OUTSIDE function f() will not change...
    >
    > free(num2);
    > }
    >
    >
    >
    > main()
    > {
    > int *x = malloc( sizeof(int));
    > *x = 5;
    >
    > printf("x = %d\nExecuting f()...\n", *x);
    >
    > f(x); //calling f() to change the value of x
    > printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE
    >
    > free(x);
    > }

    C has no pass-by-reference no matter what anyone says. The C paradigm is
    pass-by-value. That the value of a pointer may reference an object, we
    still pass a value. I have altered your code example..

    #include <stdio.h>
    #include <stdlib.h>

    void f(int **num) {
    **num = 88;
    }

    int main(void) {
    int *x = malloc(sizeof (int));
    if (x == NULL) exit(EXIT_FAILURE);
    *x = 5;
    printf("*x = %d\nExecuting f()...\n", *x);
    f(&x); // calling f() to change the value of *x
    printf("*x = %d\n", *x); // NO ERROR: *x DID CHANGE
    free(x);
    return 0;
    }

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Nov 11, 2007
    #6
  7. ramif

    Richard Guest

    Chris Dollin <> writes:

    > Richard wrote:
    >
    >> ramif <> writes:
    >>
    >>> Does call by reference principle apply to pointers??
    >>> Is there a way to pass pointers (by reference) to functions?

    >>
    >> Someone will be along to say there is no call by reference.

    >
    > s/will/was/.
    >
    >> This is kind of true.

    >
    > As in, true up to arguments about definitions.
    >
    >> But misleading and confuses nearly everyone.

    >
    > It confuses people who have had it explained to them by confused
    > people.
    >
    >> You can pass a pointer to an object which for all

    >
    > Most.
    >
    >> intents and meaning is a "reference" for the greater majority
    >> of programmers.

    >
    > Indeed.
    >
    >> So for me, passing a pointer is indeed pass by reference.

    >
    > And here are the definitional things. No, passing a pointer
    > isn't pass (or call) by reference. You can do all the things
    > that pass by reference does by passing pointers, /except/
    > the doing-it-implicitly that's part of the definition of
    > pass-by-reference.


    Which does not apply to C. The word "reference" has a meaning in the
    real world.

    >
    >> No matter what the standard says : word games
    >> dont get the point across.

    >
    > Exactly. Your confusion of terms doesn't make C have pass-
    > by-reference, in which the construction and dereference of
    > the necessary pointers is implicit and part of the machinery
    > of the language. C happens to have those constructions and
    > dereferences as directly accessible features, so it /doesn't
    > need/ to have pass-by-reference.


    As a reasonable C++ programmer in the past as well as other languages I
    don't have a problem understanding to do with references in terms of
    those languages. But in the C world talk of reference and dereference is
    fairly clear cut IMO.

    It is not goobledook to suggest in the English language that a pointer
    references an object. It is just something I see time and time again and
    I dont see the need to fight it.

    >
    > In a language like (the original) Pascal, you couldn't
    > simulate pass-by-reference with the rest of the language
    > features, and so it was much clearer that pass-by-reference
    > was a specific feature.
    Richard, Nov 11, 2007
    #7
  8. ramif

    ramif Guest

    ramif wrote:
    > Does call by reference principle apply to pointers??
    > Is there a way to pass pointers (by reference) to functions?
    >
    > Here is my code:
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    >
    >
    > // f() is supposed to change num's value to 88...
    > void f(int *num)
    > {
    > int *num2 = malloc( sizeof(int) );
    > *num2 = 88;
    >
    > //*num = 88; //data OUTSIDE f() is changed
    > num = num2; //data OUTSIDE function f() will not change...
    >
    > free(num2);
    > }
    >
    >
    >
    > main()
    > {
    > int *x = malloc( sizeof(int));
    > *x = 5;
    >
    > printf("x = %d\nExecuting f()...\n", *x);
    >
    > f(x); //calling f() to change the value of x
    > printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE
    >
    > free(x);
    > }


    thankx to all.
    ramif, Nov 11, 2007
    #8
  9. ramif

    Richard Guest

    Joe Wright <> writes:

    > ramif wrote:
    >> Does call by reference principle apply to pointers??
    >> Is there a way to pass pointers (by reference) to functions?
    >>
    >> Here is my code:
    >>
    >>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >>
    >>
    >>
    >> // f() is supposed to change num's value to 88...
    >> void f(int *num)
    >> {
    >> int *num2 = malloc( sizeof(int) );
    >> *num2 = 88;
    >>
    >> //*num = 88; //data OUTSIDE f() is changed
    >> num = num2; //data OUTSIDE function f() will not change...
    >>
    >> free(num2);
    >> }
    >>
    >>
    >>
    >> main()
    >> {
    >> int *x = malloc( sizeof(int));
    >> *x = 5;
    >>
    >> printf("x = %d\nExecuting f()...\n", *x);
    >>
    >> f(x); //calling f() to change the value of x
    >> printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE
    >>
    >> free(x);
    >> }

    > C has no pass-by-reference no matter what anyone says. The C paradigm
    > is pass-by-value. That the value of a pointer may reference an object,


    But you see in the real world we reference memory or objects or things.
    I think if someone does not understand dereferencing a pointer then
    being clever about passing by value when you KNOW they are talking about
    passing a pointer or "reference" to an object is plain silly.

    > we still pass a value. I have altered your code example..
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > void f(int **num) {
    > **num = 88;
    > }
    >
    > int main(void) {
    > int *x = malloc(sizeof (int));
    > if (x == NULL) exit(EXIT_FAILURE);
    > *x = 5;
    > printf("*x = %d\nExecuting f()...\n", *x);
    > f(&x); // calling f() to change the value of *x
    > printf("*x = %d\n", *x); // NO ERROR: *x DID CHANGE
    > free(x);
    > return 0;
    > }
    Richard, Nov 11, 2007
    #9
  10. ramif

    Chris Dollin Guest

    Richard wrote:

    > It is not goobledook to suggest in the English language that a pointer
    > references an object. It is just something I see time and time again and
    > I dont see the need to fight it.


    Neither do I, and I'm not.

    That doesn't make calling something "pass by reference" when it
    isn't a wise idea: instead, it provides needless opportunities
    for confusion.

    --
    IMAO Hedgehog
    Nit-picking is best done among friends.
    Chris Dollin, Nov 11, 2007
    #10
  11. ramif

    Richard Guest

    Chris Dollin <> writes:

    > Richard wrote:
    >
    >> It is not goobledook to suggest in the English language that a pointer
    >> references an object. It is just something I see time and time again and
    >> I dont see the need to fight it.

    >
    > Neither do I, and I'm not.
    >
    > That doesn't make calling something "pass by reference" when it
    > isn't a wise idea: instead, it provides needless opportunities
    > for confusion.


    I fully understand and agree with what and why you are saying. I just
    don't think it should need to said - if some new C programmer asks about
    pass by reference then (to me and I fully acknowledge I am very liberal
    with language) I would say "Yes. There are more than one meaning of
    "reference" but in C you can pass a pointer or "reference" to an object
    by taking its address and passing that into a function". In 99.999% of
    the times this is the functionality or access mechanism they are looking
    for. I have no doubt I am wrong in terms of pure Computer Science (in
    which I am qualified btw) or The C Standard - but in real life situations
    I have never known anyone being confused by this representation. It's
    plain common sense. This pointer "references" an "object". You
    "dereference a parameter which is a pointer to an object".
    Richard, Nov 11, 2007
    #11
  12. ramif

    ramif Guest

    ramif wrote:
    > Does call by reference principle apply to pointers??
    > Is there a way to pass pointers (by reference) to functions?
    >
    > Here is my code:
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    >
    >
    > // f() is supposed to change num's value to 88...
    > void f(int *num)
    > {
    > int *num2 = malloc( sizeof(int) );
    > *num2 = 88;
    >
    > //*num = 88; //data OUTSIDE f() is changed
    > num = num2; //data OUTSIDE function f() will not change...
    >
    > free(num2);
    > }
    >
    >
    >
    > main()
    > {
    > int *x = malloc( sizeof(int));
    > *x = 5;
    >
    > printf("x = %d\nExecuting f()...\n", *x);
    >
    > f(x); //calling f() to change the value of x
    > printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE
    >
    > free(x);
    > }



    http://publib.boulder.ibm.com/infoc.../com.ibm.xlcpp8a.doc/language/ref/cplr233.htm

    The above website contains two articles of what IBM thinks about "pass
    by reference" and "pass by value". (I am now a little bit confused...)

    I have one final question: Does this website contains misleading
    information?
    ramif, Nov 11, 2007
    #12
  13. ramif

    ramif Guest

    ramif wrote:
    > Does call by reference principle apply to pointers??
    > Is there a way to pass pointers (by reference) to functions?
    >
    > Here is my code:
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    >
    >
    > // f() is supposed to change num's value to 88...
    > void f(int *num)
    > {
    > int *num2 = malloc( sizeof(int) );
    > *num2 = 88;
    >
    > //*num = 88; //data OUTSIDE f() is changed
    > num = num2; //data OUTSIDE function f() will not change...
    >
    > free(num2);
    > }
    >
    >
    >
    > main()
    > {
    > int *x = malloc( sizeof(int));
    > *x = 5;
    >
    > printf("x = %d\nExecuting f()...\n", *x);
    >
    > f(x); //calling f() to change the value of x
    > printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE
    >
    > free(x);
    > }



    http://publib.boulder.ibm.com/infoc.../com.ibm.xlcpp8a.doc/language/ref/cplr233.htm

    The above website contains two articles of what IBM thinks about "pass
    by reference" and "pass by value". (I am now a little bit confused...)

    I have one final question: Does this website contains misleading
    information?
    ramif, Nov 11, 2007
    #13
  14. ramif

    Richard Guest

    ramif <> writes:

    >
    >
    > http://publib.boulder.ibm.com/infoc.../com.ibm.xlcpp8a.doc/language/ref/cplr233.htm
    >
    > The above website contains two articles of what IBM thinks about "pass
    > by reference" and "pass by value". (I am now a little bit
    > confused...)


    It refers to what I said. Using a pointer to pass by reference. It's how
    millions and millions of people in the real programming world think of
    "pass by reference" when using C.

    C does not "pass by reference" but it "allows pass by reference
    through the use of a pointer to an object". Anything more picky is being
    clever for clevers sake.

    >
    > I have one final question: Does this website contains misleading
    > information?


    IMO No.
    Richard, Nov 11, 2007
    #14
  15. On Sun, 11 Nov 2007 22:51:51 +0100, ramif wrote:

    >http://publib.boulder.ibm.com/infoc.../com.ibm.xlcpp8a.doc/language/ref/cplr233.htm
    >
    >The above website contains two articles of what IBM thinks about "pass
    >by reference" and "pass by value". (I am now a little bit confused...)
    >
    >I have one final question: Does this website contains misleading
    >information?


    The C version of 'swapnum' is obviously not correct.


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
    Roland Pibinger, Nov 11, 2007
    #15
  16. On Sun, 11 Nov 2007 19:03:13 +0100, Richard wrote:
    >Joe Wright writes:
    >> C has no pass-by-reference no matter what anyone says. The C paradigm
    >> is pass-by-value. That the value of a pointer may reference an object,

    >
    >But you see in the real world we reference memory or objects or things.


    Even "Java is strictly pass-by-value, exactly as in C":
    http://javadude.com/articles/passbyvalue.htm


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
    Roland Pibinger, Nov 11, 2007
    #16
  17. ramif

    Chris Dollin Guest

    Richard wrote:

    > Chris Dollin <> writes:
    >
    >> Richard wrote:
    >>
    >>> It is not goobledook to suggest in the English language that a pointer
    >>> references an object. It is just something I see time and time again and
    >>> I dont see the need to fight it.

    >>
    >> Neither do I, and I'm not.
    >>
    >> That doesn't make calling something "pass by reference" when it
    >> isn't a wise idea: instead, it provides needless opportunities
    >> for confusion.

    >
    > I fully understand and agree with what and why you are saying.


    Splendid! We rescue agreements from, er, something.

    > I just don't think it should need to said - if some new C programmer
    > asks about
    > pass by reference then (to me and I fully acknowledge I am very liberal
    > with language) I would say "Yes. There are more than one meaning of
    > "reference" but in C you can pass a pointer or "reference" to an object
    > by taking its address and passing that into a function".


    I think our main difference here is that I'd start with "No, but" rather
    than "yes". (I wouldn't use the same /words/ for the rest, but the spirit
    would be much the same.)

    > In 99.999% of
    > the times this is the functionality or access mechanism they are looking
    > for.


    99.9999% of statistics in newsgroups are made up, but I agree that
    /usually/ they're looking to update a variable that appears as an
    argument.

    > I have no doubt I am wrong in terms of pure Computer Science (in
    > which I am qualified btw)


    It's just the traditional definition. Perhaps I'm too much the
    traditionalist.

    > or The C Standard


    I don't /think/ the Standard says anthing about call-by-reference
    (since C doesn't have it [he said circularly] it doesn't need to).

    > - but in real life situations
    > I have never known anyone being confused by this representation. It's
    > plain common sense. This pointer "references" an "object". You
    > "dereference a parameter which is a pointer to an object".


    That bit we'd already agreed, I thought.

    --
    Pointy Spines Hedgehog
    "Who do you serve, and who do you trust?" /Crusade/
    Chris Dollin, Nov 12, 2007
    #17
    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. S?ren Gammelmark
    Replies:
    1
    Views:
    1,879
    Eric Sosman
    Jan 7, 2005
  2. Marc Thrun
    Replies:
    15
    Views:
    857
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    306
    August Karlstrom
    Sep 24, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    668
  5. ajaybgr
    Replies:
    18
    Views:
    963
    Philip Lantz
    Sep 7, 2012
Loading...

Share This Page