1

Problem space

Hi Guys,

Why would commenting the prototype function char *zalloc(); give the compilation error below?

Everything seems to be working fine once the prototype function comment is removed.

Example code:

#include <stdio.h>

#define ALLOCSIZE 10000

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf; // allocbuf = &allocbuf[0]

char *fn();
// char *zalloc();

int main()
{
    int a = 100;

    char *c = fn();
    char *d = zalloc(1000000);

    printf("%s\n", c);
    printf("%p\n", d);
}

char *zalloc(int n)
{
    if (allocbuf + ALLOCSIZE - allocp >= n) {
        allocp += n;
        return allocp - n;
    } else
        return 0;
}

void afree(char *p)
{
    if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
        allocp = p;
}

char *fn()
{
    return "foo";
}

Compiler error:

example_24.c: In function 'main':
example_24.c:16:12: warning: initialization makes pointer from integer without a cast
  char *d = zalloc(1000000);
            ^
example_24.c: At top level:
example_24.c:22:7: error: conflicting types for 'zalloc'
 char *zalloc(int n)
       ^
example_24.c:16:12: note: previous implicit declaration of 'zalloc' was here
  char *d = zalloc(1000000);
2
  • 1
    In modern C, the disgnostic message should be completely different: calling an undeclared function zalloc. Calling undeclared functions is illegal in C. Your compiler is simply not configured to formally follow the requirements of language specification - that is the reason for the messages you got. Commented Oct 16, 2016 at 16:34
  • @alk Sorry, my bad, I'm slightly new to c so I thought that char *zalloc(int n) that returns a pointer, was a function pointer, apparently(if I'm not wrong), function pointers seem to be pointers that point to a function. =) Commented Oct 16, 2016 at 16:50

4 Answers 4

2
    char *d = zalloc(1000000);

In this line you're calling a function the compiler has never seen before. When that happens, the compiler assumes the function returns int, as if you had done int zalloc(int); before.

That's why you get a warning about initializing a pointer (char *d) with an integer and a "conflicting types" error later when the compiler sees the real function definition.

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

Comments

2

If the C compiler detects a function it does not know, it assumes it to return an int.

As the result of zalloc() is used to initialise a char* you get the message in question: ... makes pointer from integer ...

Comments

1

In C language you are required to declare your functions before calling them (a prototype is not required, but at least a non-prototype declaration is). Which means that when you commented-out the function declaration, your program became invalid.

Your compiler's diagnostic messages are misleading, since they seem to gloss over the primary issue. I'd guess that your compiler is not configured properly to obey the requirements of modern C specification.

P.S. Note, BTW, that both of your original function declarations are non-prototypes. You need explicit (void) as function parameter list to create a prototype declaration for a parameter-less function (referring to fn).

2 Comments

Indeed, I was trying gcc on windows cmd(just to see how it behaves there), then switched to gnu/linux(which I usually use) and ran gcc -Wall source.c -o out I got implicit declaration of function warnings if functions declared after main(), but it did compile and work.
@dud3: That means that your version of GCC either uses a C dialect based on C89/90 or still allows implicit declarations in a more modern dialect of C. If you want to use GCC for C programming, you generally need a -std=... option (e.g -std=c11) and -pedantic-errors option in the command line.
0

It doesn't compile because the compiler automatically assumes a function that's not 'function-prototyped' will return an int.

Plus, it's considered good practice to func prototype.

6 Comments

"it's considered good practice to func prototype." Why? :-)
No, the compiler only assumes that for undeclared functions. It doesn't need a prototype.
@Nergal, @alk One silly question, doesn't the char on zalloc() tell what would the function return? If so how can we return 0 from zalloc(), is it because 0 has a special meaning for pointer?
@dud3: The 0 get's implicitly converted to the null-pointer if returned. Also "on zalloc()" there isn't just a char but a char*, a pointer to char.
@alk I see, I guess return NULL would be more obvious? One more silly question, is there any specific reason for 0 to be equal to NULL when dealing with pointers?
|

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.