Incompatible types: cannot convert from

Game Editor comments and discussion.

Incompatible types: cannot convert from

Postby peedalein » Mon Dec 20, 2010 8:17 pm

Greetings,

I am trying to write a simple code that is placed in global script and returns a random string.

My first step was to write the following code.

Code: Select all
char RandomStringTest()
{
    char random="test";
    return random;
}


However, this gives me the following error "Incompatible types: cannot convert from 'const * char' to 'char'"

What do I need to do that the code returns "test"?
peedalein
 
Posts: 54
Joined: Mon May 17, 2010 2:26 pm
Score: 1 Give a positive score

Re: Incompatible types: cannot convert from

Postby lcl » Mon Dec 20, 2010 8:52 pm

Code: Select all
char random[255];

char * RandomStringTest()
{
    sprintf(random, "test");
    return random;
}


This code works.
You have to make the string as global variable. That's the first line in my code.
Then the function must be declared with * 'cause it returns string.
Then, the sprintf function sets random's text to be test.
And then the code returns random. :D

I hope this helped you! :D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Incompatible types: cannot convert from

Postby peedalein » Thu Dec 23, 2010 11:17 pm

Hi,

the code helped. Thanks a lot for your effort.

However, I encountered some new problem.

I would like the code to randomly return one of the two strings but I get an error message -> Flow reaches end of non-void function "RandomStringTest".



Code: Select all
char random1[255];
char random2[255];
int randomnumber;

char * RandomStringTest()

    {
    randomnumber = rand(2);
 
    sprintf(random2, "notest");
    sprintf(random1, "test");
 
    if(randomnumber == 0) {
    return random1;};

    if(randomnumber == 1) {
    return random2;};
peedalein
 
Posts: 54
Joined: Mon May 17, 2010 2:26 pm
Score: 1 Give a positive score

Re: Incompatible types: cannot convert from

Postby lcl » Sat Dec 25, 2010 2:03 am

Code: Select all
char temp[255];

char * RandomStringTest()
{
    int randomnumber = rand(2);
 
    switch(randomnumber)
    {
        case 0:
            sprintf(temp, "test");
        break;
 
        case 1:
            sprintf(temp, "notest");
        break;
    }

    return temp;
}

This code works. :D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest