Reversing a c-string with a function

This is a discussion on Reversing a c-string with a function within the C++ Programming forums, part of the General Programming Boards category; Hi guys, Well, once again I'm pulling my freaking hair out over C++ programs, you think that I would have ...

  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,344
    >> 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,344
    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, 09:18 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM

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