Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-22-2003, 07:28 AM   #1 (permalink)
socomm
Registered User
 
socomm's Avatar
 
Join Date: Apr 2003
Location: Slums of Los Angeles
Posts: 4
socomm is on a distinguished road
Send a message via AIM to socomm
Post Character conversion

All right this is my first post so be gentle. Here is my problem I am looking to convert a const char* to a char * and vice versa. All ideas, links, or flames are welcomed.

Code:
const char* x;
char* y;

y = x;
What is the proper way to convert a const char to a char?
socomm is offline   Reply With Quote
Old 04-22-2003, 07:40 AM   #2 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
they way you have it at the moment y will just recieve the address of x, not sure if this is really what you want. if you don't the syntax for assignment with pointers is

Code:
*y = *x;
now the const keyword is defined to mean that the value will never be able to be changed by your program, you can intialize it but then it is really read only.

edit: though it should be possible to reassign the value that x points to.
palin is offline   Reply With Quote
Old 04-22-2003, 07:54 AM   #3 (permalink)
socomm
Registered User
 
socomm's Avatar
 
Join Date: Apr 2003
Location: Slums of Los Angeles
Posts: 4
socomm is on a distinguished road
Send a message via AIM to socomm
Arrow

Yes

Code:
const char* x;
char* y = *x;
invalid conversion from const char to char*
though
Code:
const char* x;
char* y;

*y = *x;
will compile just fine( with the exception that the program crashes upon launch).
socomm is offline   Reply With Quote
Old 04-22-2003, 12:02 PM   #4 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
please post your whole program.
palin is offline   Reply With Quote
Old 04-22-2003, 12:41 PM   #5 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
have you tried
const char* x = "A";

I have gotten it to work properly this way. More on this if you are still stuck when I get home from work.
palin is offline   Reply With Quote
Old 04-22-2003, 01:44 PM   #6 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
what are you trying to accomplish? you have a pointer of type 'const char *' and you want a pointer of type 'char *' to point to the same thing? this is a bad idea. things are marked const for a reason (usually because they are read-only, or supposed to be), and therefore assigning it to a non const will mean trouble.

Code:
void foo()
{
  const char *x = "dude";
  char *y;

  y = (char *)x; // cast to type 'char *'
  cout << y << endl;
}
note that in this case, if you try to modify y (or x), you will crash your program.

Code:
  y = (char *)x; // cast to type 'char *'
  y[0] = 'r'; // segmentation fault, crash
joe_bruin is offline   Reply With Quote
Old 04-22-2003, 04:16 PM   #7 (permalink)
agh
Registered User
 
Join Date: Apr 2003
Posts: 30
agh is on a distinguished road
You could do this:

Code:
void f()
{
    char c = 'a';    
    const char* ccp = &c;
    char* cp = const_cast<char*>(ccp);
    *cp = 'b';
}
But, as joe_bruin said, you shouldn't do that, because things are marked const for a reason.
agh is offline   Reply With Quote
Old 04-22-2003, 05:08 PM   #8 (permalink)
socomm
Registered User
 
socomm's Avatar
 
Join Date: Apr 2003
Location: Slums of Los Angeles
Posts: 4
socomm is on a distinguished road
Send a message via AIM to socomm
Smile Thanks

Thank you all, I am not changing the value of const char it is merely for display purposes. Thanks again I really appreciate all the help.
socomm is offline   Reply With Quote
Old 05-27-2003, 07:29 PM   #9 (permalink)
socomm
Registered User
 
socomm's Avatar
 
Join Date: Apr 2003
Location: Slums of Los Angeles
Posts: 4
socomm is on a distinguished road
Send a message via AIM to socomm
Exclamation

This is an old post but here is a solution I found,
Code:
char *fps = NULL;
int x;

stringstream ss;
ss<<x;
fps = (char *)ss.str( ).c_str( );

cout<<"FPS: "<<fps<<endl;
for future references .
socomm is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
void * conversion problems Lovecraft Standard C, C++ 6 04-28-2004 03:51 AM
invalid conversion paul Standard C, C++ 3 02-02-2004 10:58 AM
What's the windows-1252 character set for? verto Lounge 2 10-07-2003 03:57 PM
Character Counting Program DemosthenesB Standard C, C++ 8 04-03-2003 05:10 PM


All times are GMT -8. The time now is 11:28 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting