LinuxQuestions.org
Review your favorite Linux distribution.
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 03-04-2010, 03:27 PM   #1
primerib
Member
 
Registered: Mar 2010
Posts: 48

Rep: Reputation: 20
Help fixing C++ invalid conversion from const char* to char


Hi. I'm getting the following error when trying to compile:
Quote:
$ make
g++ -march=athlon64 -O2 -pipe -fomit-frame-pointer -msse3 -c -D_GNU_SOURCE -DPLUGIN_NAME_I18N='"undelete"' -DHAVE_SVDRP -I../../../include undelete.c
undelete.c: In member function ‘virtual cString cPluginUndelete::SVDRPCommand(const char*, const char*, int&)’:
undelete.c:945: error: invalid conversion from ‘const char*’ to ‘char*’
make: *** [undelete.o] Error 1
The offending line undelete.c:945 is:
Quote:
944: cString NewName = recording->FileName();
945: char *ext = strrchr(NewName, '.');
The entire undelete.c can be found at: http://pastebin.com/aTUFB6vE

To say I'm not a C++ coder would be giving me too much credit so anyone willing to help, please know that this is very new to me. Thanks!
 
Old 03-04-2010, 03:29 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by primerib View Post
Hi. I'm getting the following error when trying to compile:


The offending line undelete.c:945 is:


The entire undelete.c can be found at: http://pastebin.com/aTUFB6vE

To say I'm not a C++ coder would be giving me too much credit so anyone willing to help, please know that this is very new to me. Thanks!
What is the return type of 'strrchr' ?
 
Old 03-04-2010, 03:52 PM   #3
primerib
Member
 
Registered: Mar 2010
Posts: 48

Original Poster
Rep: Reputation: 20
I forgot to mention I'm using cpp (Debian 4.4.2-9) 4.4.3 20100108 (prerelease) in debian testing linux.

Not sure if either of these is what you're looking for but I found,

in /usr/include/c++/4.4/cstring
Code:
  inline char*
  strrchr(char* __s1, int __n)
  { return __builtin_strrchr(const_cast<const char*>(__s1), __n); }
and in /usr/include/string.h
Code:
#ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
extern "C++"
{
extern char *strrchr (char *__s, int __c)
     __THROW __asm ("strrchr") __attribute_pure__ __nonnull ((1));
extern __const char *strrchr (__const char *__s, int __c)
     __THROW __asm ("strrchr") __attribute_pure__ __nonnull ((1));

# ifdef __OPTIMIZE__
__extern_always_inline char *
strrchr (char *__s, int __c) __THROW
{
  return __builtin_strrchr (__s, __c);
}

__extern_always_inline __const char *
strrchr (__const char *__s, int __c) __THROW
{
  return __builtin_strrchr (__s, __c);
}
# endif
}
#else
extern char *strrchr (__const char *__s, int __c)
     __THROW __attribute_pure__ __nonnull ((1));
#endif
__END_NAMESPACE_STD
Thanks for your reply!

Btw, in c++/4.4/cstring I also see:
Code:
#define _GLIBCXX_CSTRING 1

// Get rid of those macros defined in <string.h> in lieu of real functions.
#undef strrchr
and

Code:
_GLIBCXX_BEGIN_NAMESPACE(std)
using ::strrchr;

Last edited by primerib; 03-04-2010 at 03:54 PM.
 
Old 03-04-2010, 04:09 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by primerib View Post
I forgot to mention I'm using cpp (Debian 4.4.2-9) 4.4.3 20100108 (prerelease) in debian testing linux.

Not sure if either of these is what you're looking for but I found,

in /usr/include/c++/4.4/cstring
Code:
  inline char*
  strrchr(char* __s1, int __n)
  { return __builtin_strrchr(const_cast<const char*>(__s1), __n); }
and in /usr/include/string.h
Code:
#ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
extern "C++"
{
extern char *strrchr (char *__s, int __c)
     __THROW __asm ("strrchr") __attribute_pure__ __nonnull ((1));
extern __const char *strrchr (__const char *__s, int __c)
     __THROW __asm ("strrchr") __attribute_pure__ __nonnull ((1));

# ifdef __OPTIMIZE__
__extern_always_inline char *
strrchr (char *__s, int __c) __THROW
{
  return __builtin_strrchr (__s, __c);
}

__extern_always_inline __const char *
strrchr (__const char *__s, int __c) __THROW
{
  return __builtin_strrchr (__s, __c);
}
# endif
}
#else
extern char *strrchr (__const char *__s, int __c)
     __THROW __attribute_pure__ __nonnull ((1));
#endif
__END_NAMESPACE_STD
Thanks for your reply!

Btw, in c++/4.4/cstring I also see:
Code:
#define _GLIBCXX_CSTRING 1

// Get rid of those macros defined in <string.h> in lieu of real functions.
#undef strrchr
and

Code:
_GLIBCXX_BEGIN_NAMESPACE(std)
using ::strrchr;
I suggest to first obtain preprocessed input file in order to better understand which of the implementations 'strrchr' is used.


The issue is moot in a sense there is a number of places where both 'const char *' and 'char *' are used.

So, you can also/first try to rewrite the code this way:

Code:
char *ext =
strrchr
  (
  NewName, 
  '.'
  );
- in such a manner hopefully the compiler will point you to the exact place where the problem is, i.e. whether it dislikes conversion at 'NewName' or at 'ext = strrchr', i.e. whether it's the argument or the result.
 
Old 03-04-2010, 04:20 PM   #5
primerib
Member
 
Registered: Mar 2010
Posts: 48

Original Poster
Rep: Reputation: 20
Quote:
Originally Posted by Sergei Steshenko View Post
I suggest to first obtain preprocessed input file in order to better understand which of the implementations 'strrchr' is used.
I just search the entire source tree and found a reference to "#include <string.h>" in several files, no references to cstring.h anywhere so I guess string.h it is.

Quote:
So, you can also/first try to rewrite the code this way:

Code:
char *ext =
strrchr
  (
  NewName, 
  '.'
  );
- in such a manner hopefully the compiler will point you to the exact place where the problem is, i.e. whether it dislikes conversion at 'NewName' or at 'ext = strrchr', i.e. whether it's the argument or the result.
I tried this and it fails with "undelete.c:951: error: invalid conversion from ‘const char*’ to ‘char*’" on the " );" line.
 
Old 03-04-2010, 04:35 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by primerib View Post
I just search the entire source tree and found a reference to "#include <string.h>" in several files, no references to cstring.h anywhere so I guess string.h it is.



I tried this and it fails with "undelete.c:951: error: invalid conversion from ‘const char*’ to ‘char*’" on the " );" line.
Well, try brute force (i.e. type cast) then:

Code:
(char *)strrchr
- even if it works, it doesn't look clean. I.e. it's better to understand then what functionally for constant was intended.
 
Old 03-04-2010, 05:11 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Here's the problem:
Quote:
Quote:
Offending code:
944: cString NewName = recording->FileName();
945: char *ext = strrchr(NewName, '.');
Quote:
Prototype for C strchr:
char *strchr(const char *s, int c);
Here's a reasonable solution:
Quote:
char *ext = strrchr((const char *)NewName, '.');
There are a number of stylist and/or technical issues one could debate (for example, using an "old fashioned" C-style cast instead of a C+ "const_cast<>") but, frankly, I think the above snippet should fix the problem, and be perfectly OK for you.

'Hope that helps .. PSM
 
Old 03-04-2010, 05:23 PM   #8
primerib
Member
 
Registered: Mar 2010
Posts: 48

Original Poster
Rep: Reputation: 20
It seems to compile now, thank you. I'm not sure what the author is using a constant at all. I've tried emailing him but there was no response. Unfortunately I'm guessing the intent is something a more knowledgeable person then myself would have to look into.

Thanks again for the help!
 
Old 03-04-2010, 05:26 PM   #9
primerib
Member
 
Registered: Mar 2010
Posts: 48

Original Poster
Rep: Reputation: 20
Quote:
Originally Posted by paulsm4 View Post
Hi -

Here's the problem:

Here's a reasonable solution:


There are a number of stylist and/or technical issues one could debate (for example, using an "old fashioned" C-style cast instead of a C+ "const_cast<>") but, frankly, I think the above snippet should fix the problem, and be perfectly OK for you.

'Hope that helps .. PSM
I tried your solution of char *ext = strrchr((const char *)NewName, '.'); but it actually gave me the same error as in the original post.
 
Old 03-04-2010, 05:50 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by primerib View Post
It seems to compile now, thank you. I'm not sure what the author is using a constant at all. I've tried emailing him but there was no response. Unfortunately I'm guessing the intent is something a more knowledgeable person then myself would have to look into.

Thanks again for the help!
Which of the two solutions worked - suggested by paulsm4 or myself ?

The approach is the same, the point is to understand what the compiler actually meant. I.e. to learn how to identify the exact problematic spot.
 
Old 03-04-2010, 05:57 PM   #11
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
One more thing: Sergei Steshenko and I are both curious which of these work for you:
Code:
  const char *ext = strrchr((const char *)NewName, '.');  // A
Code:
  char *ext = strrchr((const char *)NewName, '.');  // B
Code:
  const char *ext = strrchr(NewName, '.');  // and/or C
Thanx in advance .. PSM

PS:
Again, the prototype I have for "strrchr()" looks like this:
Quote:
man strrchr =>
#include <string.h>
...
char *strrchr(const char *s, int c);
It sounds like yours is different

Last edited by paulsm4; 03-04-2010 at 06:00 PM.
 
Old 03-04-2010, 06:00 PM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by paulsm4 View Post
One more thing: I'm curious which of these work for you:
...
- we've just asked the same question.
 
Old 03-04-2010, 06:09 PM   #13
primerib
Member
 
Registered: Mar 2010
Posts: 48

Original Poster
Rep: Reputation: 20
doesn't work:
const char *ext = strrchr((const char *)NewName, '.'); // A
char *ext = strrchr((const char *)NewName, '.'); // B
const char *ext = strrchr(NewName, '.'); // and/or C

works
char *ext = (char *)strrchr(NewName, '.');


So what does that tell us?
 
Old 03-04-2010, 06:14 PM   #14
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
const char *ext = strrchr(NewName, '.');
Should have infact worked, HMMM...
 
Old 03-04-2010, 06:15 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by primerib View Post
doesn't work:
const char *ext = strrchr((const char *)NewName, '.'); // A
char *ext = strrchr((const char *)NewName, '.'); // B
const char *ext = strrchr(NewName, '.'); // and/or C

works
char *ext = (char *)strrchr(NewName, '.');


So what does that tell us?
It tells that the function returns 'const char *' (if I'm not mistaken).

But my point was that it looks like when the error message is tied to ');', it is about function return type, not argument type.
 
  


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
invalid conversion from `const char*' to `char' error holystinken Programming 7 11-23-2009 06:01 PM
error: invalid conversion from const char* to char* Dahakon Programming 1 08-31-2009 09:33 AM
about C++ invalid conversion from 'const char*' to 'char' teoporta Programming 3 07-17-2007 09:24 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

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

All times are GMT -5. The time now is 07:35 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