Is there a way to convert from const char * to char * without casting away the const? I was warned that it could be dangerous and cause crashes (which I would agree with, since when I ran my program with const_cast, it caused a segmentation fault). Alternatively, is there a way to convert from string to char * (not convert from string to const char *, as c_str() does)? Any suggestions would be greatly appreciated.

// Read the wordlist file

  ifstream wordlist("./samplewordlist.txt");


  Dictionary htl; 

  if(wordlist)
    {
      cout << "The wordlist file was opened." << endl;
      string line;
      wordlist.ignore(7, '\n');  // ignore the first entry (the number of words in the wordlist)
      while(getline(wordlist, line, '\n'))
	{
	  cout << "Line = " << line << endl; 
	  // convert from string to char *
	  const char * constConvline = line.c_str();
	  // convert from const char * to char * 
	  char * convline;
	  convline = const_cast<char *>(constConvline);
	  htl.add(convline);
	  cout << "added " << convline << endl;
	}
    }
  else if(!wordlist)
    {
      cerr << "The wordlist could not be read." << endl;
      exit(2);
    }

>Is there a way to convert from const char *
>to char * without casting away the const?
Short answer: No.

>I was warned that it could be dangerous and cause crashes
It's const for a reason. For example, when you point to a string literal, the pointer should be declared as "const char *" because string literals are stored in read-only memory. Casting away the const won't change the read-only nature of the data itself, and trying to modify it will probably cause a crash.

You should only cast away const if you're so sure that the chamber is empty, you'd be willing to point the gun at your face and pull the trigger without any hesitation. In all other cases, work around the const. Make a copy, for example:

char *pline = new char[line.size() + 1];

strcpy ( pline, line.c_str() );
htl.add ( pline );
cout<<"added "<< pline <<'\n';

delete[] pline;

>Alternatively, is there a way to convert from string to char * (not convert from string to const char *, as c_str() does)?
Yes there is:

string s1 = "Hello World";
char *s2 = new char[s1.size()+1];
strcpy(s2, s1.c_str());
delete s2;

explanation of the code:

line 1: declare a string and put some sample data in it
line 2: dynamically allocate memory (one element extra because of the NULL-terminator)
line 3: create a copy from the constant string and put it into a non-constant character array
line 4: free up dynamically allocated memory

Narue,

Thank you so much for explaining that to me! It was really helpful. Also, I was super excited when I saw that your homepage was eternallyconfuzzled because I saw it before and bookmarked it! The internet (that discusses C++) is a small world :3

tux4life,

Thanks a lot for providing your explanation! It really helped and I really appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.