[Solved] Return makes integer from pointer without a cast (simple for loop) [duplicate]


Your function is declared to return char and you are returning an array of char. But even if you fix the return value which should be char * it’s still wrong because you should not return a local variable, instead pass the array as an argument to the function like this

void
n_zeroes(int n, char *str)
{
    for (int i = 0 ; i < n ; ++i)
        str[i] = '0';
}

but this is essentially the same as

memset(str, '0', n);

also, note that this array filled just with zeroes does not represent a string, to make it a valid string that you can use with str* functions or pass to *printf() functions with the "%s" specifier it needs one extra element at the end with the specific value of '\0' or 0.

As you can understand from the error your code is making a pointer, from an integer without an explicit cast. This is because you are allowed to do that, i.e. to cast an integer to a pointer. Whether that’s a correct thing to do or not is entirely a different thing, but the standard allows many things that can be misused too. That’s the great thing about c, with great power comes greate responsibility.

8

solved Return makes integer from pointer without a cast (simple for loop) [duplicate]