pass by value or pass by address?

Discussion in 'C++' started by Vols, Apr 28, 2008.

  1. Vols

    Vols Guest

    void f (char *a)
    {
    a++;
    }

    int main (void)
    {
    char a[] = "abc";// or char *a = "abc";
    f (a);
    puts (a);
    return 0;
    }

    Looks it is 'pass by address'. The result is 'abc' instead of 'bc'. So
    actually it is 'pass by value'.
    What happened? thanks.

    Vol
     
    Vols, Apr 28, 2008
    #1
    1. Advertising

  2. Vols

    Ian Collins Guest

    Vols wrote:
    > void f (char *a)
    > {
    > a++;
    > }
    >
    > int main (void)
    > {
    > char a[] = "abc";// or char *a = "abc";
    > f (a);
    > puts (a);
    > return 0;
    > }
    >
    > Looks it is 'pass by address'. The result is 'abc' instead of 'bc'. So
    > actually it is 'pass by value'.
    > What happened? thanks.
    >

    It's pass by value. You are passing the value of the pointer a.

    --
    Ian Collins.
     
    Ian Collins, Apr 28, 2008
    #2
    1. Advertising

  3. Vols

    Vols Guest

    Then if I want to output 'bc' instead of 'abc', how to write f()?
    Thanks.
    Vol.
    On Apr 27, 4:24 pm, Ian Collins <> wrote:
    > Vols wrote:
    > > void f (char *a)
    > > {
    > > a++;
    > > }

    >
    > > int main (void)
    > > {
    > > char a[] = "abc";// or char *a = "abc";
    > > f (a);
    > > puts (a);
    > > return 0;
    > > }

    >
    > > Looks it is 'pass by address'. The result is 'abc' instead of 'bc'. So
    > > actually it is 'pass by value'.
    > > What happened? thanks.

    >
    > It's pass by value. You are passing the value of the pointer a.
    >
    > --
    > Ian Collins.
     
    Vols, Apr 28, 2008
    #3
  4. Vols

    Ian Collins Guest

    Vols wrote:

    {please don't top-post]

    > On Apr 27, 4:24 pm, Ian Collins <> wrote:
    >> Vols wrote:
    >>> void f (char *a)
    >>> {
    >>> a++;
    >>> }
    >>> int main (void)
    >>> {
    >>> char a[] = "abc";// or char *a = "abc";
    >>> f (a);
    >>> puts (a);
    >>> return 0;
    >>> }
    >>> Looks it is 'pass by address'. The result is 'abc' instead of 'bc'. So
    >>> actually it is 'pass by value'.
    >>> What happened? thanks.

    >> It's pass by value. You are passing the value of the pointer a.
    >>

    > Then if I want to output 'bc' instead of 'abc', how to write f()?


    [or quote signatures]

    void f( char*& a)
    {
    a++;
    }

    --
    Ian Collins.
     
    Ian Collins, Apr 28, 2008
    #4
    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. =?Utf-8?B?QW5nZWw=?=

    Pass a Web Address and return a File System Address

    =?Utf-8?B?QW5nZWw=?=, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    548
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=
    Sep 21, 2005
  2. Jerry
    Replies:
    20
    Views:
    7,987
    Roedy Green
    Sep 9, 2005
  3. blufox
    Replies:
    2
    Views:
    575
  4. venkatagmail
    Replies:
    11
    Views:
    698
    James Kanze
    Oct 3, 2007
  5. Tricky
    Replies:
    0
    Views:
    591
    Tricky
    Mar 19, 2009
Loading...

Share This Page