Shinz6
Shinz6

Reputation: 147

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 ?

Upvotes: 1

Views: 14514

Answers (1)

alk
alk

Reputation: 70903

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.

Upvotes: 5

Related Questions