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);
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.cso I thought thatchar *zalloc(int n)that returns a pointer, was afunction pointer, apparently(if I'm not wrong),function pointersseem to be pointers that point to a function. =)