LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2007, 02:19 PM   #1
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
casting a const char into a char


What happens when you cast a const char* to a char*?

Code:
const char* str1 = "test";
char *str2 = (char*) str1;
Is str2 now a new string? Does it need to be freed?
 
Old 01-12-2007, 03:18 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

Short answer: no

But I'll try to explain. Strings in C are confusing. It's usually better to think of them as an array of characters.

These lines are equivalent:
Code:
const char *str = "test";
const char str[5] = { 't','e','s','t','\0' };
Code:
char *str2 = (char*) str1;
This makes a new pointer variable called str2 and it points to str1 (the 't' character). If you try to modify that string, you will get a segmentation fault.

To make a new string that's a pointer to heap memory that you can modify and then free, use the strdup function.

Code:
char *str2 = strdup(str1);
The strdup allocates memory for the string with malloc, and then copies the string to that memory, and finally it returns a pointer to the new allocated memory.

Last edited by Guttorm; 01-12-2007 at 03:19 PM.
 
Old 01-12-2007, 03:24 PM   #3
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Original Poster
Rep: Reputation: 31
Right. I saw this in some code, and I can't figure out why the author wanted to cast out the const'ness of the string
 
Old 01-12-2007, 07:26 PM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by MicahCarrick
Right. I saw this in some code, and I can't figure out why the author wanted to cast out the const'ness of the string
well it would make sense if the string in question is not a literal.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
char* const* (C++) anamericanjoe Programming 5 12-19-2006 07:28 AM
invalid conversion from `const char*' to `char*' deepinlife Programming 22 08-05-2006 10:49 AM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
c++ ERROR: int to const char ashirazi Programming 6 08-06-2004 10:01 PM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration