2

I'm coded function which splits 2 char arrays and integer into one string and returns pointer to new char array, but when compiling I'm getting this warning:

warning: assignment makes pointer from integer without a cast

mapfile = world_to_char( "maps/", map, ".bin" );

             ^

Here is my code, first two are lines from file where gcc is giving warning to me:

char *mapfile;
mapfile = world_to_char( "maps/", map, ".bin" );

and function (I know that it works only with numbers from [0;99], but it's enough for me):

char *world_to_char( char dir[], int number, char ext[] ) {
    char id[ 3 ] = { 0, 0, 0 };

    if( number > 9 ) {
        id[ 1 ] = ( number % 10 ) + '0';
        number /= 10;
    }
    id[ 0 ] = number + '0';

    char *map;
    map = malloc( MAX_MAP_FILENAME * sizeof( char ) );

    strcpy( map, dir );
    strcat( map, id );
    strcat( map, ext );

    return map;
}

Why I'm getting this warning? I'm returning pointer not integer.

2
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. Commented Jan 8, 2016 at 22:15
  • If you're using a C99 compiler, you should be getting warnings about calling a function that was not previously declared. If you're not using a C99 or later compiler, or you're not getting the warnings, find the options that give you the warning. If you're using GCC, consider using options as stringent as gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror. You can be more stringent if you like. Commented Jan 9, 2016 at 7:26

1 Answer 1

3

It's likely that world_to_char() is defined after main(), add a function prototype and it should work. So before main() add this

char *world_to_char(char *dir, int number, char *ext);

the parameters with "java" style char dir[] will become pointers anyway, so let's make it clear by changing them to char *dir instead.

The error message is because of implicit declaration, since the compiler haven't seen a declaration when you call the function it assumes that it returns int, so assigning int to char * is making a pointer from an integer without a cast.

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

2 Comments

A declaration would suffice: char *world_to_char(); :-)
@Szeldon It's a common mistake, your question was really well written and formatted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.