-1

I'm reading an old book called C Traps and Pitfalls by A Koenig, published in 1989, just before the first C standard. In it, I find the code below:

char *r, *malloc();
r = malloc(strlen(s) + strlen(t) + 1);

The first line can't be compiled correctly; I use CodeBlocks with MinGW to compile it, which gives me the following error message:

||=== Build: Debug in beta (compiler: GNU GCC Compiler) ===|
C:\Users\ADMIN\Desktop\beta\beta\main.c||In function 'main':|
C:\Users\ADMIN\Desktop\beta\beta\main.c|9|error: conflicting types for 'malloc'|
C:\Program Files (x86)\CodeBlocks\MinGW\include\stdlib.h|356|note: previous declaration of 'malloc' was here|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

I have learnt some C, but I don't know the meaning of "char *malloc()" and why the compiler gives out the error.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
E000N
  • 11
  • 3
    How old is that book? You should not declare standard function yourself but include the correct header instead. Since you seem to include the correct header (``) which include a proper declaration of the `malloc` function, your declaration is conflicting with that. – Some programmer dude Jul 13 '17 at 08:16
  • 4
    Discard that book, asap. – StoryTeller - Unslander Monica Jul 13 '17 at 08:16
  • `malloc` is a reserved method for c found in stdlib.h, therefore it creates a confliction. – Taha Paksu Jul 13 '17 at 08:17
  • If you want some *good* books, I suggest you check [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Some programmer dude Jul 13 '17 at 08:18
  • 2
    `error: conflicting types for 'malloc'` is quite to the point. – David C. Rankin Jul 13 '17 at 08:25
  • Just for completeness: multiple declaration in a single line are a thing from the past, when terminal screens were tiny. It's just bad style now, having **one** declaration gives much better readability. Declaring a variable **and** a function in one line is a whole other level of evil... –  Jul 13 '17 at 08:27
  • 2
    It's a 1989 book. C has changed a lot since then. If you are not *very* familiar with these changes, you are better off not reading this book. – n. m. could be an AI Jul 13 '17 at 08:45
  • @TahaPaksu The conflict comes from including `stdlib.h`, not just because it is a 'reserved method'. – user207421 Aug 11 '17 at 00:22
  • @EJP I meant that by reserved :) Sorry for the inconvenience. – Taha Paksu Aug 11 '17 at 06:16

1 Answers1

7
char *malloc();

Is a K&R C function declaration. We call it K&R C because it is the C dialect described in the first edition of The C Programming Language by Kernighan and Ritchie which was published in around 1980. That declaration says "malloc exists, returns char* and may or may not take parameters".

In 1990, the first official C standard was published. One of the biggest improvements over K*R style C was the introduction of function prototypes in which the types of the parameters can also be declared. Another, was the introduction of the void type and therefore also void *. Since 1990, the C header file stdlib.h has had the following declaration for malloc

void *malloc(size_t size);

This conflicts with your redeclaration in having a different return type, hence the error.

Your book C Traps and Pitfalls is 27 years old and three standards out of date (C90, C99, C11). Get a new book.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Your answer teaches me a lot. Thank you so much! I have read c primer plus 5th edition. While I'm reading this book, I just forget that how old it is and wondering if there is a new gamma that I don't know OTL. I won't be confused next time and I think this is an old book, but still worth reading. I will also be appreciated if you could recommend me some book about details part or anything needs to consider while programming about c. – E000N Jul 14 '17 at 11:07