22

I have a block of code where I'm trying to grab an expression inside of parentheses and then use it. At the point where the below code begins, I am in the middle of iterating through a character array and pcc is the pointer to the current character, which has been determined to be a '('. My goal is to put the paranthetical expression in a character array pe.

            int nnrp = 1; /* Net number of right parantheses */
            char * pbpe = pcc; /* Pointer to the beginning paranthetical expression */
            for (++pcc; *pcc!= '\0' && nnrp != 0; ++pcc)
            {
                if (*pcc == '(')
                {
                    ++nnrp;
                }
                else if (*pcc == ')')
                {
                    --nnrp;
                }
                else if (*pcc == '\0')
                {
                    sprintf(err, "Unbalanced paranthesis");
                    return -1;
                }
            }
            /* If we're here, *pcc is the closing paranathesis of *pbpe */
            long nel = pcc - pbpe; /* New expression length */
            if (nel == 1)
            {
                sprintf(err, "Empty parenthesis");
                return -1;
            }
            char * pe = (char*)malloc(nel+1); /* Paranthetical expression */
            strncpy(pcc+1, pcc, nel);
            pe[nel] = '\0';

But my IDE (XCode 6.0) is giving me the warning

"Semantic issue: Implicitly declaring library function 'malloc' with type 'void *(unsigned long)'"

on the strncpy(pcc+1, pcc, nel); line. I'm wondering

  1. why I'm getting this warning.
  2. whether I need to fix it
  3. if there are any other problems you can see in my code.

Thanks in advance.

Lily Carter
  • 273
  • 1
  • 2
  • 5
  • I'd have expected that implicit declaration to be of type `int (long)` rather than `void *(unsigned long)`... Curious: Which compiler (of which version and with which options) is this? – mafso Feb 23 '15 at 10:20
  • Clang gives this warning, with both `-std=c89` and `-std=c99`. This is not the traditional way of implicit function declarations (which was linked below), Clang chooses the _correct_ declaration of `malloc` and emits a warning (required by C99; in C89, the code is simply undefined). Interesting. – mafso Feb 23 '15 at 10:27

1 Answers1

60

Try adding this line to the top of your file:

#include <stdlib.h>

This will bring in the explicit declaration of malloc, so you shouldn't get that warning.

You are probably getting a warning because you forgot to include stdlib.h in your file. The compiler is being nice to you and giving you an implicit declaration of malloc so that the code will compile. In general, it's better to include the explicit declaration so that the compiler really knows what kind of function you are trying to call, and it's also good to fix all the warnings that you can so that your build process will be clean and you can notice the more important warnings. So yes, you should fix it.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • 1
    @LilyCarter: you should also remove the `(char *)` cast from the `malloc` call; it isn't necessary, and under older compilers will suppress a useful diagnostic if you forget to include `stdlib.h` or otherwise don't have a declaration for `malloc` in scope. Note that in C++ the cast *is* required, but if you're writing C++ you should be using the `new` operator instead of `malloc`. – John Bode Feb 23 '15 at 05:18
  • in C [a cast shouldn't be used](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). And the implicit declaration is also explained in that question – phuclv Feb 23 '15 at 06:29
  • 1
    I had the same problem. #include did not solve the problem for me, but #include did. – Baeumla Oct 02 '17 at 18:03