Thread: Converting const char to char array

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    7

    Converting const char to char array

    Hey

    I need to pass a string into a method:

    void parseData ( char data[] )

    however i can only convert the string to a const char as follows:

    const char* slk = knights.c_str();

    which the method wont accept and i cant work out how to convert this further!

    Ive searched the forum can anyone help me?

    (the string im trying to convert is a file name.)

    thanks in advance guys!

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    void parseData ( char data[] )

    What is the prototype? That doesn't sound right. If you need a const char*, try just:

    parseData(knights.c_str());

    Also, what kind of parsing are you doing, if you pass a const and don't return anything?

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'm guessing you could do this:
    char slk[80] = knights.c_str();
    parseData(slk);

    Or:
    char slk[80];
    strcpy(slk,knights.c_str());
    parseData(slk);

    But it also seems like this would work:
    parseData ( knights.c_str() ) ;

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    7
    guys ive tried both of those before, and i get no luck watsoever, for some reason this method is being very difficult but heres the error the compiler throws at me:

    'parseData' : cannot convert parameter 1 from 'const char *' to 'char []'
    Conversion loses qualifiers

    Any ideas?

    All the method does is take in a char array (filename) and reads the individual lines into a vector. (basically anyway)

    cheers for the responses

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You shouldn't make it const, this is the problem. parseData(data) will be changing data. Alternately you can change parseData() to:

    void parseData ( const char data[] )

    but then data can't be changed within the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM