Thread: Reversing a c-string with a function

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    Reversing a c-string with a function

    Hi guys,

    Well, once again I'm pulling my freaking hair out over C++ programs, you think that I would have learned my lesson last year after that horrible data structures class. Anyways, I'm reviewing my text, and I have a question the book (and a lot of tutorials) don't answer in a straightforward manner:

    If I have a c-string, what it the best way to pass it to a function (as a char*, as a char[], etc), and what is the best way to return it? Should I declare the return type as char*, char[], etc.?

    Also, for converting a c-string to a string, what is the most straightforward way?

    Thanks,

    Patrick

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> for converting a c-string to a string
    Just assign the character array to the string, the conversion is done automatically by the string class.

    >> If I have a c-string, what it the best way to pass it to a function.
    I'd use char* or const char*, but I don't think it matters whether you use char* or char[].

    >> what is the best way to return it?
    That depends on the situation. C style strings are hard to return because you have to deal with the memory issues.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Quote Originally Posted by Daved View Post
    >> C style strings are hard to return because you have to deal with the memory issues.
    Thanks, Daved!

    OK, so, then, book exercises aside, C-strings are best converted to strings before returning (and in general, too, correct?).

    -Patrick

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes. In fact, you almost never need to use a C style string in a C++ program. In most situations I would use C++ strings exclusively.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM