简体   繁体   中英

invalid conversion from `const char*' to `char*'

#include <stdio.h>
#include <string.h>
#include <conio.h>
#define SIZE 20

int main( void )
{
    int n; //number of characters to be compared
    char s1[ SIZE ], s2[ SIZE ];
    char *results_word;

    printf( "Enter two strings: " );
    gets( s1 );
    gets( s2 );
    printf( "\nEnter the number of characters to be compared: " );
    scanf( "%d", &n );

The problem starts here

    results_word = 
                 strncmp( s1, s2, n ) > 0 ? " greater than " : 
                 strncmp( s1, s2, n ) == 0 ? " equal to " : " smaller than " ;

    printf( "\n%sis%s%s", s1, results_word, s2 );

    getche();
    return 0;
}//end function main

So why doesn't result_word get the corresponding string ?

The C++ error message you are getting says it all:

invalid conversion from `const char*' to `char*'

You are trying to assign some constant "<literal>" to the non constant results_word .

Change

char *results_word;

to be

const char *results_word;

and it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM