-1

I am coding a game in which i have to store a word in an array then the function returns that word. In my case the word is "control", the array name is mot_generer, and the function name is initialisation_mot_a_trouver.

I have tried to use the code return mot_generer but i have the error code below:

jeu_pendu.c: In function ‘initialisation_mot_a_trouver’: jeu_pendu.c:82:10: warning: return makes integer from pointer without a cast [-Wint-conversion] return mot_generer;

char initialisation_mot_a_trouver(){  
    char mot_generer[]="controle";  
    return mot_generer;
}
4
  • 3
    You can't because the string is a local variable that ceases to exis when the function returns. Use malloc. Commented Sep 20, 2019 at 9:27
  • 2
    Futher, the function returns a char but you return a char array. Declare as char *init.. Commented Sep 20, 2019 at 9:28
  • i think this issue is already solved here Commented Sep 20, 2019 at 9:36
  • If you actually have an array what is initialized by a string constant, you can make the local variable to a static variable. This is a different approach as mentioned in the answer, what tell you to return a heap object what must be free()ed. You can change the function signature to return a const char* and return then static char mot_generer[] = "initialized-with-a-compile-time-defined-string"; Commented Sep 20, 2019 at 10:27

3 Answers 3

0

See my comments. The correct version would be:

char *initialisation_mot_a_trouver()
{  
    char *mot_generer= malloc(strlen("controle")+1);
    strcpy(mot_generer, "controle");
    return mot_generer;
}

The caller must free() the string returned.

Sign up to request clarification or add additional context in comments.

Comments

0

There are two errors in the function.

The first one is that arrays in expressions are implicitly converted to pointers to element types of the arrays.

So if the function has the return expression

return mot_generer;

where mot_generer is an array then the function return type should be char *.

The second error is that the function (correctly declared) returns pointer to a local array with the automatic storage duration that will not be aluve after exiting the function.

So either declare the array within the function as having the static storage duration like

char * initialisation_mot_a_trouver(){  

    static char mot_generer[]="controle";  

    return mot_generer;
}

Or allocate the array dynamically like

char * initialisation_mot_a_trouver()
{
    const char *literal = "controle";

    char *s = malloc( strlen( literal ) + 1 );

    if ( s != NULL ) strcpy( s, literal );

    return s;
}

A third approach is to pass to the function an already created array in main.

In this case the function can look like

char * initialisation_mot_a_trouver( char s[], size_t n )
{
    strncpy( s, "controle", n );
    s[n -1] = '\0';

    // or for example
    // fgets( s, n, stdin );
    // s[ strcspn( s, "\n" ) ] = '\0'; 

    return s;
}

Comments

-1

Your function says it would return a char, but in fact you are returning a char*.

char* initialisation_mot_a_trouver(){  

    char mot_generer[]="controle";  

    return mot_generer;

}

However I do not return local stack addresses since they may easily change during the exeuction of the application. Better use heap addresses:

char* initialisation_mot_a_trouver(){  
    char mot_generer[]="controle";
    char *pszNamePtr = calloc(strlen(mot_generer) + 1, sizeof(char));
    strncpy(pszNamePtr, mot_generer, strlen(mot_generer);

    return pszNamePtr;

}

Or check out Paul Ogilvie's answer for another approach.

8 Comments

1) calloc is not needed; 2) don't cast the return value of malloc; 3) sizeof(char) is always 1; 4) strncpy is not needed (and then you should always set the terminating null)
Returning pointers to local variables is not only "not recommended" but it is blatently wrong.
@PaulOgilvie 1) what is needed and what not does not benefit me or the op you can achieve things in multiple ways 3) still not wrong 4) same as 1) + with calloc I dont have to set it and I make sure the data is initialized to zero. But I appreciate your 2)
@Trickzter you are right about strncpy, sorry my bad, but a solution does not only have to work correctly (as yours obviously does) but it should also be simple and efficient. Compare your bloated error prone code with the simple code of Paul Ogilvie's answer.
ad 1) calloc returns zeroed memory. Having the memory zeroed is not needed; there is no benefit whatsoever (just more machine cycles to zero the memory). ad 3) just more chars to type without a clear benefit. 4) the memory is precisely allocated. There is no need to use a limiting function. And as said, if you use it, then use it correctly and always put in the zero terminator. Also a call to strlen is not needed and again only costs more cycles.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.