Like Tree8Likes

Modify string in function

This is a discussion on Modify string in function within the C++ Programming forums, part of the General Programming Boards category; How to modify the string that I pass to the function that I get it back as modified. I wanna ...

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

    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;
    }
    Elysia likes this.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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
    796
    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.
    Elysia, Ducky and R41D3N like this.

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

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    But you can, a reference to a reference is just a reference.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    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
    796
    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
    21,935
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    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];
    King Mir likes this.
    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,053
    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.

Page 1 of 6 123456 LastLast
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, 07:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21