LinuxQuestions.org
Help answer threads with 0 replies.
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 08-04-2006, 02:18 AM   #1
deepinlife
Member
 
Registered: Apr 2006
Posts: 78

Rep: Reputation: 15
invalid conversion from `const char*' to `char*'


guys
how can i elminate such error..

i use
g++ 3.3.6
kernel 2.4.31
distrubtion slackware 10.2

i always face this error and i solve it by make casting but i m asking about new soultion or some flags i pass to g++ to solve it?
 
Old 08-04-2006, 03:05 AM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Can you give the code you're trying to compile? Depending on what you're doing this might be an issue with the code rather than the compiler.

const char* is definitely not the same as char*
 
Old 08-04-2006, 03:09 AM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Yeah, it sounds like a violation of const-correctness.
 
Old 08-04-2006, 04:00 AM   #4
deepinlife
Member
 
Registered: Apr 2006
Posts: 78

Original Poster
Rep: Reputation: 15
Code:
char  meta_data::decode_int( const char* current ,int* charecter_lenght , long* integer )
{
	//make sure that it is integer
	if( *current != 'i' )
		return ERROR;
	//get the output of the integer
	const char *x = current+1 ;
	*integer = strtol( x , (char**)0 , 10 );
	;
	//check for underflow and overflow of strtol
	if( *integer ==  LONG_MAX || *integer==  LONG_MIN )
		return ERROR;
	
	//get  lenght
	while( true )
	{
		if(*(x++) == 'e')
			break;
	}
	*charecter_lenght = x-current ;
	return OK;
}
 
Old 08-04-2006, 04:22 AM   #5
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Seems fine to me. Can you show us the rest of the code and the exact error message and verify that the error is from this function? Maybe it's from the code that calls this function?
 
Old 08-04-2006, 06:21 AM   #6
deepinlife
Member
 
Registered: Apr 2006
Posts: 78

Original Poster
Rep: Reputation: 15
Code:
#include <iostream>

#include "../code/meta_data.h"
#include "../code/const.h"

using namespace std;

int main()
{
	meta_data  META;
	char array[] ="i55466466543e";
	int size=0;
	long lenght=0;
	char error = META.decode_int( array , &size ,&lenght);
	if( error!=ERROR)
		cout << "array =" << array<<endl
			<<"size= " << size <<"\t lenht = "<< lenght << endl;
	else cout<<"error\n";
	
	return 0;
}

Code:
class meta_data
{
	long lenght;

	public:
		char decode_int( const char* current ,int* charecter_lenght , long* integer );
};
 
Old 08-04-2006, 06:25 AM   #7
deepinlife
Member
 
Registered: Apr 2006
Posts: 78

Original Poster
Rep: Reputation: 15
i m sorry but i forgot to tell about the error message..
in the function defination
u see that i have declared
const char* x;
if i deleted the const word ,the error will appear..
invalid conversion from const char* to char*
 
Old 08-04-2006, 06:38 AM   #8
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Since you're using C++ I strongly recommend std::string instead of char* or const char*. You can always pass an std::string.c_str() to library functions which need const char* arguments and it's much less hassle to use std::string in all your programs since you needn't allocate and deallocate memory each time like you do for char*.

edit: since you're already using namespace std, it should be a matter of just declaring it as

string a_str = "A string";

Last edited by vharishankar; 08-04-2006 at 06:42 AM.
 
Old 08-04-2006, 07:52 AM   #9
deepinlife
Member
 
Registered: Apr 2006
Posts: 78

Original Poster
Rep: Reputation: 15
thx Harishankar for the string advice..i will try to reconsider using char pointer , but i still want to know about the pointer problem now..

i know that c++ is so restrict about conversion but i need to know how to overcome it
 
Old 08-04-2006, 09:35 AM   #10
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Ehmmm.., actually I don't see you problem:
Code:
const char *x = current+1 ;
You declared current as const char* so you have to declare x also as const, because there is no impicite conversion from const char* to char* (exactly what the error message told you). If you necessarily need to assign a "const" to a "non-const", then you can use the explicite convert operator const_cast:
Code:
const char *current;
char *x;
x = const_cast<char*>(current);
But be careful; very often you should consider to change your design instead of using const_cast. You may also want to go through some tutorials about type casting as well as the const modfier in C++. Simply ask google for these words.

PS. And as Harishankar said already: You should consider to use std::string
 
Old 08-05-2006, 12:53 AM   #11
deepinlife
Member
 
Registered: Apr 2006
Posts: 78

Original Poster
Rep: Reputation: 15
thx everyone .. i always use casting to solve this problem , but i m asking about another kind of solution..i mean is there a flag i pass to g++ which elminate reporting this kind of error?
 
Old 08-05-2006, 01:01 AM   #12
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Const correctness is a good thing. You are making a promise that you are not going to modify the stuff something points to, etc. So either you keep the promise, or you don't make it in the first place. What's so hard about that?
 
Old 08-05-2006, 04:40 AM   #13
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
you declare: char array[] ="i55466466543e";

but call: meta_data::decode_int( const char* current ,int* charecter_lenght , long* integer )

and get: invalid conversion from `const char*' to `char*'

and for me it should be: invalid conversion from `char*' to `const char*'

It should not be a proble with const char to char but a problem with char to const char.

Strange?
 
Old 08-05-2006, 04:43 AM   #14
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
maybe its her: const char *x = current+1 ;

maybe the "current" needs to be declared as a char.
 
Old 08-05-2006, 05:03 AM   #15
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by kalleanka
you declare: char array[] ="i55466466543e";

but call: meta_data::decode_int( const char* current ,int* charecter_lenght , long* integer )

and get: invalid conversion from `const char*' to `char*'

and for me it should be: invalid conversion from `char*' to `const char*'

It should not be a proble with const char to char but a problem with char to const char.

Strange?
No, you can always do that. Do you understand what the const means? "const char *" means you cannot do anything that can modify the things pointed to by the pointer, which is more restrictive than "char *", so it's always okay to convert from "char *" to "const char *", but not the other way.
 
  


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
error: invalid conversion from `void*' to `char**', where should I start to look for RHLinuxGUY Programming 4 05-20-2006 08:53 PM
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
Conversion from char[100] to char* ? zahadumy Programming 2 12-11-2005 09:04 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 09:04 AM.

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