Using a String in the Place of a Char *

This is a discussion on Using a String in the Place of a Char * within the C++ Programming forums, part of the General Programming Boards category; I'm playing around with a function that requires a char * as input. The function reads some information and dumps ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    15

    Using a String in the Place of a Char *

    I'm playing around with a function that requires a char * as input. The function reads some information and dumps it into the char *. I was wondering if there is a way to use a string in the place of a char *. It's not really necessary but I was just kind of curious.

    Thanks in advance,

    P

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    If you've got a string, you can easily turn it into a char* by doing strname.c_str(). Is that what you're looking for?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Unfortunately, no, std::string::c_str() returns a const char*, and the function in question modifies the C-style string. Furthermore, at the moment the characters stored in std::string are not required to be contiguous (unlike the elements of std::vector), so you cannot safely just pass &str[0] to the function, where str is the name of your std::string.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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