pass by value

Discussion in 'C Programming' started by mdh, May 20, 2007.

  1. mdh

    mdh Guest

    May I just confirm my understanding that:

    If a pointer is passed as an argument to a function, manipulation of
    the function pointer ( eg pointer ++ till some condition....) will not
    effect the value of the calling pointer?

    So;

    myf(p) /* p is ptr to char) */


    void myf( char *q){

    while (...)

    *q++;

    }

    leaves p unchanged?
    mdh, May 20, 2007
    #1
    1. Advertising

  2. mdh

    osmium Guest

    "mdh" writes:

    > May I just confirm my understanding that:
    >
    > If a pointer is passed as an argument to a function, manipulation of
    > the function pointer ( eg pointer ++ till some condition....) will not
    > effect the value of the calling pointer?
    >
    > So;
    >
    > myf(p) /* p is ptr to char) */
    >
    >
    > void myf( char *q){
    >
    > while (...)
    >
    > *q++;
    >
    > }
    >
    > leaves p unchanged?


    Yes, that's right. One nitpick, I wouldn't use "function pointer" in this
    discussion, it usually means something else.
    osmium, May 20, 2007
    #2
    1. Advertising

  3. mdh

    mdh Guest

    On May 20, 10:33 am, "osmium" <> wrote:
    > "mdh" writes:
    > > May I just confirm my understanding that:

    >
    > > If a pointer is passed as an argument to a function, ........



    > One nitpick, I wouldn't use "function pointer" in this
    > discussion, it usually means something else.



    Good point...thank you
    mdh, May 20, 2007
    #3
  4. mdh said:

    > May I just confirm my understanding that:
    >
    > If a pointer is passed as an argument to a function, manipulation of
    > the function pointer ( eg pointer ++ till some condition....) will not
    > effect the value of the calling pointer?
    >
    > So;
    >
    > myf(p) /* p is ptr to char) */
    >
    >
    > void myf( char *q){
    >
    > while (...)
    >
    > *q++;
    >
    > }
    >
    > leaves p unchanged?


    It does indeed leave p unchanged. It doesn't change anything in the
    thing pointed to, either, though. Were you aware of that? It is
    possible that *q++; doesn't do quite what you think it does.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 21, 2007
    #4
  5. mdh

    Richard Guest

    Richard Heathfield <> writes:

    > mdh said:
    >
    >> May I just confirm my understanding that:
    >>
    >> If a pointer is passed as an argument to a function, manipulation of
    >> the function pointer ( eg pointer ++ till some condition....) will not
    >> effect the value of the calling pointer?
    >>
    >> So;
    >>
    >> myf(p) /* p is ptr to char) */
    >>
    >>
    >> void myf( char *q){
    >>
    >> while (...)
    >>
    >> *q++;
    >>
    >> }
    >>
    >> leaves p unchanged?

    >
    > It does indeed leave p unchanged. It doesn't change anything in the
    > thing pointed to, either, though. Were you aware of that? It is
    > possible that *q++; doesn't do quite what you think it does.


    In this sample code that is misleading.

    the "..." could have been anything manipulating exactly what p was
    pointing to.
    Richard, May 21, 2007
    #5
  6. Richard said:

    > Richard Heathfield <> writes:
    >
    >> mdh said:
    >>

    <snip>
    >>>
    >>> So;
    >>>
    >>> myf(p) /* p is ptr to char) */
    >>>
    >>>
    >>> void myf( char *q){
    >>>
    >>> while (...)
    >>>
    >>> *q++;
    >>>
    >>> }
    >>>
    >>> leaves p unchanged?

    >>
    >> It does indeed leave p unchanged. It doesn't change anything in the
    >> thing pointed to, either, though. Were you aware of that? It is
    >> possible that *q++; doesn't do quite what you think it does.

    >
    > In this sample code that is misleading.


    No, it isn't.

    >
    > the "..." could have been anything manipulating exactly what p was
    > pointing to.


    Indeed, but we can only go on the code that is posted. My point refers
    specifically to the statement: *q++;

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 21, 2007
    #6
  7. mdh

    mdh Guest

    On May 21, 8:12 am, Richard Heathfield <> wrote:
    > Richard said:
    >
    >
    >
    >
    >
    > >> mdh said:

    >
    > <snip>
    >
    > >>> So;

    >
    > >>> myf(p) /* p is ptr to char) */

    >
    > >>> void myf( char *q){

    >
    > >>> while (...)

    >
    > >>> *q++;

    >
    > >>> }

    >
    > >>> leaves p unchanged?

    >
    > >> It does indeed leave p unchanged. It doesn't change anything in the
    > >> thing pointed to, either, though. Were you aware of that?




    Nope...but then I was just "making up" some code to be sure I
    understood "pass by value" and in the past, I have had my knuckles
    rapped for doing this!!! :)
    As I look at the hasty code I wrote, I think what this would do would
    simply point at subsequent values of an array and not change a
    thing!!! The wonder of terse code :)
    mdh, May 22, 2007
    #7
  8. mdh

    mdh Guest

    On May 21, 7:58 pm, CBFalconer <> wrote:

    >> Nonsense. q is passed by value. Diddling it, or what it points

    > to, will not alter p.
    >


    Neither Richard(I believe) nor I were talking about altering p.
    mdh, May 22, 2007
    #8
    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. Jerry
    Replies:
    20
    Views:
    7,901
    Roedy Green
    Sep 9, 2005
  2. venkatagmail
    Replies:
    11
    Views:
    670
    James Kanze
    Oct 3, 2007
  3. Vols
    Replies:
    3
    Views:
    363
    Ian Collins
    Apr 28, 2008
  4. Tricky
    Replies:
    0
    Views:
    563
    Tricky
    Mar 19, 2009
  5. Tricky
    Replies:
    0
    Views:
    985
    Tricky
    Mar 19, 2009
Loading...

Share This Page