Passing string types to a function

Discussion in 'C++' started by Suddn, Nov 12, 2003.

  1. Suddn

    Suddn Guest

    Help me get my mind around passing string types to a function.

    I need to have the function modify the string types and get them back.
    Normaly I would just return the modified string but I need to modify about
    five different strings.

    I thought that strings were passed by reference in C/C++ but when I pass
    the strings in they remain unaltered in the calling function. I did test
    the function being called and they are being althered there.

    Can someone please show me the syntax that I need.

    i.e.

    void foo(string s, string s2)
    {

    }

    int main(int argc, char *argv[])
    {
    string a, b;
    foo(a, b);
    return 0
    }

    Or whatever is correct (the above doesn't work.)

    Thanks.
     
    Suddn, Nov 12, 2003
    #1
    1. Advertising

  2. Suddn

    Derek Baker Guest

    void foo(string& s, string& s2)
    {

    }

    --
    Derek
    "Suddn" <> wrote in message
    news:p...
    > Help me get my mind around passing string types to a function.
    >
    > I need to have the function modify the string types and get them back.
    > Normaly I would just return the modified string but I need to modify about
    > five different strings.
    >
    > I thought that strings were passed by reference in C/C++ but when I pass
    > the strings in they remain unaltered in the calling function. I did test
    > the function being called and they are being althered there.
    >
    > Can someone please show me the syntax that I need.
    >
    > i.e.
    >
    > void foo(string s, string s2)
    > {
    >
    > }
    >
    > int main(int argc, char *argv[])
    > {
    > string a, b;
    > foo(a, b);
    > return 0
    > }
    >
    > Or whatever is correct (the above doesn't work.)
    >
    > Thanks.
     
    Derek Baker, Nov 12, 2003
    #2
    1. Advertising

  3. Suddn wrote:
    > ...
    > I thought that strings were passed by reference in C/C++ but when I pass
    > the strings in they remain unaltered in the calling function. I did test
    > the function being called and they are being althered there.
    > ...


    The compiler will pass the strings the way you asked it to pass them. If
    you direct the compiler to pass them by value, it will pass them by
    value. The same applies to passing by reference.

    > void foo(string s, string s2)


    This function will receive its arguments by value. In order to receive
    them by reference you have to declare it as follows

    void foo(string& s, string& s2)

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 12, 2003
    #3
  4. Suddn

    Jon Bell Guest

    In article <>,
    Suddn <> wrote:
    >
    >I thought that strings were passed by reference in C/C++


    That refers to C-style strings which are just arrays of char. Arrays in
    general are passed implicitly by reference.

    C++ style strings behave like "normal" data types as far as passing them
    to functions is returned. If you want to change the value inside the
    function and have that change communicated back to the calling function,
    pass it by reference, or by pointer. The same applies for vectors, by the
    way, if you're using them instead of arrays (which you probably should).

    >void foo(string s, string s2)
    >{
    >
    >}


    void foo (string& s string& s2)

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Nov 12, 2003
    #4
  5. Suddn

    jeffc Guest

    "Suddn" <> wrote in message
    news:p...
    >
    > I thought that strings were passed by reference in C/C++ but when I pass
    > the strings in they remain unaltered in the calling function. I did test
    > the function being called and they are being althered there.
    >
    > Can someone please show me the syntax that I need.
    >
    > i.e.
    >
    > void foo(string s, string s2)
    > {
    >
    > }
    >
    > int main(int argc, char *argv[])
    > {
    > string a, b;
    > foo(a, b);
    > return 0
    > }


    void foo(string& s, string& s2)
     
    jeffc, Nov 12, 2003
    #5
    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. Sathyaish
    Replies:
    2
    Views:
    602
    Sathyaish
    May 22, 2005
  2. James Vanns
    Replies:
    7
    Views:
    7,084
    Evan Carew
    Jan 21, 2004
  3. Shawn
    Replies:
    1
    Views:
    316
    Chris Torek
    Jan 16, 2006
  4. manu
    Replies:
    5
    Views:
    393
    Nick Keighley
    Sep 17, 2009
  5. ittium
    Replies:
    4
    Views:
    310
    Goran
    Dec 9, 2011
Loading...

Share This Page