LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack Search this Thread
Old 08-31-2009, 09:29 AM   #1
Dahakon
LQ Newbie
 
Registered: Aug 2009
Posts: 1

Rep: Reputation: 0
Red face error: invalid conversion from const char* to char*


First, I have already looked at the other answers on this topic, and can not figure it out. I am trying to convert code from C to C++. The error I am getting is invalid conversion from const char* to char* in string.c. Below is my code.

Code:
void string_add( CHAR_DATA *ch,  const char *argument)
{
    char buf[MAX_STRING_LENGTH];
    char local_argument[MAX_INPUT_LENGTH];
    char *lap = local_argument;

    /*
     * Thanks to James Seng
     */
    strcpy(local_argument, argument);
    smash_tilde( local_argument );

    if ( *lap == '.' )
    {
        char arg1 [MAX_INPUT_LENGTH];
        char arg2 [MAX_INPUT_LENGTH];
        char arg3 [MAX_INPUT_LENGTH];
        char tmparg3 [MAX_INPUT_LENGTH];

        lap = one_argument( lap, arg1 );
   -> Error     lap = first_arg( lap, arg2, FALSE );
        strcpy( tmparg3, lap );
   -> Error     lap = first_arg( lap, arg3, FALSE );
 (Comment: Since both errors seem to be with first_arg, that code will be added below.)
        if ( !str_cmp( arg1, ".c" ) )
        {
            send_to_char( "String cleared.\n\r", ch );
            free_string(*ch->desc->pString);
            *ch->desc->pString = str_dup( "" );
            return;
        }

        if ( !str_cmp( arg1, ".s" ) )
        {
            send_to_char( "String so far:\n\r", ch );
            send_to_char( numlineas(*ch->desc->pString), ch );
            return;
        }





const char *first_arg( char *argument, char *arg_first, bool fCase )
{
    char cEnd;

    while ( *argument == ' ' )
        argument++;

    cEnd = ' ';
    if ( *argument == '\'' || *argument == '"'
      || *argument == '%'  || *argument == '(' )
    {
        if ( *argument == '(' )
        {
            cEnd = ')';
            argument++;
        }
        else cEnd = *argument++;
    }

    while ( *argument != '\0' )
    {
        if ( *argument == cEnd )
        {
            argument++;
            break;
        }
    if ( fCase ) *arg_first = LOWER(*argument);
            else *arg_first = *argument;
        arg_first++;
        argument++;
    }
    *arg_first = '\0';

    while ( *argument == ' ' )
        argument++;
    return argument;
}

There is all of it, I just can not figure out where I am screwing up, any help would be appreciated! If you need anymore information please let me know. Thank you!
 
Old 08-31-2009, 09:33 AM   #2
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,012

Rep: Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731
Quote:
Originally Posted by Dahakon View Post
invalid conversion from const char* to char*
Code:
    char *lap = local_argument;
Code:
lap = first_arg( lap, arg2, FALSE );
Code:
const char *first_arg( char *argument, char *arg_first, bool fCase )
first_arg() returns a const char* but lap is a char*. That is the error.

I don't know which would be easiest to change.

You seem to be using char* in several places where const char* would be more correct. For example, it looks like first_arg() modifies the characters pointed to by arg_first, but not those pointed to by argument. So it might be more correct if it were declared as

Code:
const char *first_arg( const char *argument, char *arg_first, bool fCase )
Then, you didn't show where else lap is used, so with that change to first_arg() you might fix the error by making lap const as well
Code:
 const char *lap = local_argument;
But depending on how lap is used elsewhere, that might cause further problems.

Sometimes the only way to work with existing code, in which the const / non const choices have not been made carefully, is to cheat. You can hide the error from the compiler with a const_cast. Just change the problem lines for example:

Code:
lap = const_cast<char*>( first_arg( lap, arg2, FALSE ) );
If the code was logically correct and just had errors in where const was used or omitted, that will stop the compiler from caring the error and get you past the problem quickly. Use such cheating with care: It may confuse you later and make other bugs harder to diagnose.

Last edited by johnsfine; 08-31-2009 at 10:00 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with "error: invalid conversion from `const char*' to `char*' " dave.f.one Linux - Newbie 10 11-19-2008 10:01 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
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM


All times are GMT -5. The time now is 04:49 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration