char *p, *malloc();
is declaring a pointer to char
named p
and a function named malloc
(In these K&R C days, void*
did not exist).
This is old Kernigan & Ritchie C (from the 1980s). The C standard has evolved significantly since that time. Current C standard is C11, and its previous version C99 is very widely used.
Don't code this way today. malloc
is defined in <stdlib.h>
and it might be some inlined function there (but usually is not).
Your function is often conventionally called xmalloc
today; you'll need to add
#include <stdlib.h>
#include <stdio.h>
in the start of your file (since malloc
and exit
is declared in <stdlib.h>
and EXIT_FAILURE
is defined there, and perror
is declared in <stdio.h>
), and would have been defined as
void* xmalloc(size_t sz) {
void* p = malloc(sz);
if (!p) {
perror("malloc");
exit(EXIT_FAILURE);
};
return p;
}
and declared in some header of yours with
void* xmalloc(size_t sz);
I suggest reading a newer book. On Linux, consider at least the Advanced Linux Programming book (which is dated from 2001).
If using GCC you could add function attributes to your function declaration.