convert const char * to char*?

This is a discussion on convert const char * to char*? within the C++ Programming forums, part of the General Programming Boards category; i cant figure out how, any ideas?...

  1. #1
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455

    convert const char * to char*?

    i cant figure out how, any ideas?


  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Try const_cast< >( )
    Here is a simple example.
    Code:
    void g (const char& c)
    {
      val = f(const_cast<char&>(c));
    }

  3. #3
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    worked, thanks

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Dont use const_cast unless you really know what you are doing....

    The reason why you are getting an error is that the string you are trying to copy the pointer of is const and so therefore should not be altered....and if you assigning it to a normal pointer, then that pointer has the ability to alter the string......

    If the string is say a string literal, and you cast away the constness and if you then try to alter that string you may cause a fault that could crash your prog.....

    Ony use const_cast if you are absolutely sure the pointer will not try change the data it points to

  5. #5
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    i want to change the data tho, its in a string type, and im doing

    char *str = mystring.c_str();

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Fine...but do it properly

    Code:
    #include <iostream>
    #include <string>
    
    
    int main(){
    
    using std::cout;
    using std::endl;
    using std::string;
    
    	std::string mystring("Hello World");
    
    	mystring.replace(0,1,"M",1);
    
    	cout << "Changed " << mystring << endl;
    
    
    }

  7. #7
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    im using it with something like this:

    Code:
    	char *pattern = const_cast<char*>(pat.c_str());
    	bool hitletter=true;
    
    	Trim(find);
    	if(strcmp(find,pattern)==0) {
    		return true;
    	}
    im gonna be working on that mroe today..but its pattern detection and stuff, but i converted them into ways i know how to work with them

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Far easier...

    Code:
    if(mystring.compare(find)==0) {
    		return true;
    	}

  9. #9
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    ahh ok, im new to string. i guess ill go find some docs

    thanks

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Typically when you need to manipulate a constant string/char array you would make a copy of that string/char array. Obviously not for this though.

  11. #11
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Your problem here is the char* datatype. string.c_str returns a const char* and strcmp takes a const char* so there is absolutly no need to have anything but a const char*.

    Code:
    const char* pattern = pat.c_str();
    
    bool hitletter=true;
    
    Trim(find);
    
    if( strcmp( find, pattern ) == 0 ) return true;
    You almost never need to cast a const to non-const. If you find you need to do this, think about your program design and why it is const in the first place. Never cast a const to non-const.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Playing card data structure?
    By crypticgeek in forum C++ Programming
    Replies: 10
    Last Post: 12-31-2006, 04:29 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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