Page 1 of 1

Incompatible types: cannot convert from

PostPosted: Mon Dec 20, 2010 8:17 pm
by peedalein
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"?

Re: Incompatible types: cannot convert from

PostPosted: Mon Dec 20, 2010 8:52 pm
by lcl
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

Re: Incompatible types: cannot convert from

PostPosted: Thu Dec 23, 2010 11:17 pm
by peedalein
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;};

Re: Incompatible types: cannot convert from

PostPosted: Sat Dec 25, 2010 2:03 am
by lcl
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