-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;
}
Mike
  • 4,041
  • 6
  • 20
  • 37
h.barry
  • 27
  • 3
  • 3
    You can't because the string is a local variable that ceases to exis when the function returns. Use `malloc`. – Paul Ogilvie Sep 20 '19 at 09:27
  • 2
    Futher, the function returns a char but you return a char array. Declare as `char *init..` – Paul Ogilvie Sep 20 '19 at 09:28
  • i think this issue is already solved [here](https://stackoverflow.com/a/4085393/7158025) – mehdigriche Sep 20 '19 at 09: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";` – harper Sep 20 '19 at 10:27

3 Answers3

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.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
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;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-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.

Trickzter
  • 471
  • 3
  • 14
  • 1
    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) – Paul Ogilvie Sep 20 '19 at 09:33
  • 1
    Returning pointers to local variables is not only "_not recommended_" but it is blatently wrong. – Jabberwocky Sep 20 '19 at 09:33
  • @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 Sep 20 '19 at 09:45
  • @Jabberwocky agree! – Trickzter Sep 20 '19 at 09:46
  • @Trickzter I suggest you adapt your answer. And `calloc` is really not needed here, because the allocated memory is being overwritten anyway, so no need to zero initialize the memory in the first place. And yes, `strncpy` is wrong here, you dont copy the NUL terminator. Using `strcpy` is good enough here, no need to call `strlen`. Why make it complicated when it can be simple? – Jabberwocky Sep 20 '19 at 09:47
  • @Jabberwocky strncpy is not wrong here since I dont need to copy the \0, because I initialized it to zero with calloc. I get your point, but forcing others to see your approach as "simpler" is wrong imho. Everyone can do as he/she wants and in fact, my solution will work. Still appreciating your malloc/strcpy combo! – Trickzter Sep 20 '19 at 09:54
  • 1
    @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. – Jabberwocky Sep 20 '19 at 09:58
  • 1
    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. – Paul Ogilvie Sep 20 '19 at 10:29