1

Having a casting error with this function strcat()

   // split the block
    for (i=0; block_size <= first_buddy_size/2; ++i) {
            first_buddy_size/=2;
            response[i] = strcat("F", itoa(first_buddy_size, buff, 10));
        }
    response[i] = strcat("A", itoa(block_size, buff, 10));

error I am getting

buddy.c: In function `process_request':
buddy.c:49: warning: passing arg 2 of `strcat' makes pointer from integer without a cast
buddy.c:51: warning: passing arg 2 of `strcat' makes pointer from integer without a cast

my declartions

 block buddy_block[BUDDY_SIZE];
 char* response[BUDDY_SIZE] = {0};
 first_buddy_size = buddy_block[0].data;
Student
  • 55
  • 2
  • 13
  • @BrianRoach No, this is a different situation here – Sebastian Hoffmann Mar 30 '14 at 22:51
  • @Paranaix No, it's the *exact* situation here. The OP is passing a `char` instead of a `char *` and getting the exact error they should. And the answer to the dup points that out. Plus I wasn't willing to spend more that 5 seconds doing the search the OP should have. – Brian Roach Mar 30 '14 at 22:53
  • 1
    Show the declaration for `itoa`. If it's in a system header, know that it's not guaranteed by any standard to be there; are you sure it exists? – jwodder Mar 30 '14 at 22:54
  • 2
    Aside from the `itoa` problem, you can't use a string literal as the first argument to `strcat` - it may not be writable, and even if it is, there's no room at the end for your appended characters to go. –  Mar 30 '14 at 22:54
  • @BrianRoach But `itoa` is supposed to return a `char*`, thus this situation is kind of odd and cant be simply solved by stating "pass a pointer, not an int/char/long whatever" – Sebastian Hoffmann Mar 30 '14 at 22:55
  • @jwodder headers `#include #include #include #include ` `first_buddy_size = buddy_block[0].data; block_size = get_block_size(atoi(s));` – Student Mar 30 '14 at 22:55
  • @Student: So `itoa` isn't a function you wrote yourself? As I said before, `itoa`'s not a standard function, so it may not actually be in any of those headers. If it's not, the compiler will just infer that it returns an `int`, which would explain your problem. – jwodder Mar 30 '14 at 22:56
  • We should mention the C FAQ entry on `itoa`: http://c-faq.com/lib/itoa.html –  Mar 30 '14 at 22:57
  • If `itoa` is not defined, there should be a warning about that also .. if not, then turn up your warning level – M.M Mar 30 '14 at 23:13

2 Answers2

2

Your problem appears to be that you haven't actually written a itoa function, and you're just assuming the compiler has one available. Unfortunately, the compiler doesn't. itoa isn't part of the C standard or the POSIX standard, and it's probably not part of any other major standard either. However, due to backwards compatibility issues, a C compiler won't complain when it encounters a reference to a function that hasn't been declared; it'll just assume that the function's arguments match the types being passed to it and that the function returns an int. Thus, although you may think itoa returns a char*, because you never actually defined it, the compiler will think it returns an int, leading to the error message shown when you try to pass that assumed int to strcat.

The simplest solution is to just define your own itoa. See this FAQ entry for advice on how.

jwodder
  • 54,758
  • 12
  • 108
  • 124
1

char * strcat ( char * destination, const char * source );

The above is the definition of strcat, the first parameter is a pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.

So in your code:

strcat("F", itoa(first_buddy_size, buff, 10))

The first parameter is wrong, "F" is a const string, which can not be modified.

It should be like this:

char str[MAX_SIZE] = {0};
str[0] = 'F';
strcat(str, itoa(first_buddy_size, buff, 10));
Matt
  • 6,010
  • 25
  • 36
  • 2
    `str[0]` is a `char` and `"F"` is a `char*`! – rullof Mar 30 '14 at 22:58
  • @rullof, sorry, just modified, thanks! – Matt Mar 30 '14 at 23:00
  • 1
    To make thinks clearer `strcat` is going to append the string pointed to by the second argument to the string pointed to by the first argument. So the second arg must not be a `const` otherwise you will get an undefined behavior because of trying to modify a read-only memory. – rullof Mar 30 '14 at 23:06