Thread: Modify string in function

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    897

    Modify string in function

    How to modify the string that I pass to the function that I get it back as modified.
    I wanna get back just "world!" but without returning anything.

    Code:
    #include <iostream>
    using namespace std;
    
    
    void f2(char * buf2)
    {
       buf2 += 6;
       cout<< buf2 <<endl;
    }
    
    int main()
    {
        char buf[88] = "Hello world!";
        f2(buf);
        cout << buf << endl;
    
        return 0;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    May I suggest using std::string instead?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    void f2( string& theString )
    {
        if ( theString.length() >= 7 )
        {
            theString = theString.substr( 6 );
        }
    }
    
    int main()
    {
        string myString = "Hello world!";
        f2( myString );
        cout << myString << endl;
    
        return 0;
    }

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pay attention to the &, which is a reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    897
    Unfortunately I use an API function (recv) that passes a char* so I need to pass a char*.
    Compiler MSVC++ 2013 with Code::Blocks.

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    In that case:

    Code:
    #include <iostream>
    using namespace std;
     
     
    void f2( char*& buf2 ) // you can have references to anything, even pointers!
    {
       buf2 += 6;
       cout<< buf2 <<endl;
    }
     
    int main()
    {
        char buf[88] = "Hello world!";
        char* ptrToArray = buf;
        f2( ptrToArray );
        cout << ptrToArray << endl;
     
        return 0;
    }
    But I'd strongly advise you to add some error checking because as it stands this code is BEGGING for buffer overruns.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    897
    Great! Thanks a lot!
    You made my day.
    Compiler MSVC++ 2013 with Code::Blocks.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can't have references to references, though! There the argument fails...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Quote Originally Posted by Elysia View Post
    You can't have references to references, though! There the argument fails...
    True. In the presence of fellow programmers, one cannot be careful enough about his choice of words. Ain't no nitpickier ilk on the planet.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,354
    But you can, a reference to a reference is just a reference.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    But you can, a reference to a reference is just a reference.
    I would not call it that. The language just treats adding a reference to a reference type as simply a reference to the type.
    There is no synctactic way to make a reference to reference, either (excluding template trickery)...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,354
    It's amazing how you can say the same thing as me, but use it in a correcting tone.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    897
    reference to array is illegal too, it looks like:

    Code:
    int function(char & pvBuffer[]);
    error C2234: 'pvBuffer' : arrays of references are illegal

    But I would need it cause I dont want to malloc() in my program.
    Compiler MSVC++ 2013 with Code::Blocks.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,692
    Ah, but if you are going to do malloc/new[] within the function, then you just need to pass a pointer by reference (or a std::vector by reference).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is an array of references, which is illegal. However, a reference to an array is perfectly legal.

    Array of references: int& arr[mysize];
    Reference to an array: int (&arr)[mysize];
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,135
    You can also do this:
    Code:
    #include <iostream>
    using namespace std;
     
    void f2( char** buf2 ) 
    {
       *buf2 += 6;
       cout<< *buf2 <<endl;
    }
     
    int main()
    {
        char buf[88] = "Hello world!";
        char* ptrToArray = buf;
        f2( &ptrToArray );//not f2(&buf)
        cout << ptrToArray << endl;
     
        return 0;
    }
    Last edited by King Mir; 06-18-2012 at 10:48 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modify function pass it array
    By a.mlw.walker in forum C Programming
    Replies: 12
    Last Post: 08-01-2011, 04:03 AM
  2. Replies: 15
    Last Post: 05-11-2011, 05:06 PM
  3. how to modify strcmp function
    By asteroid1122 in forum C Programming
    Replies: 6
    Last Post: 08-23-2009, 12:24 AM
  4. modify pointer to a string/character constant.
    By xsouldeath in forum C Programming
    Replies: 12
    Last Post: 10-03-2007, 02:41 AM
  5. modify has function from string parameter to templates...
    By rusty0412 in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2005, 08:02 PM