C Traps and Pitfalls

Category: Programming
Author: Andrew Koenig
This Year Stack Overflow 1
This Month Stack Overflow 1


by anonymous   2019-07-21
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.

by pjc50   2018-11-02
C development, like most languages, isn't really "taught" at all. You might have some teaching on the structure of the language and a basic getting started course on main(), but that's just the beginning. Everything from there up seems to be self-taught, either from the internet, code examples, or working it out from first principles.

There are a lot more risks in C than more recent languages. Koenig's book "C Traps And Pitfalls" https://www.amazon.co.uk/C-Traps-Pitfalls-Andrew-Koenig/dp/0... was extremely useful to me when I was learning the language (available at my local library!). I don't know if there's a more modern version that takes into account recent changes to the C standard.

I note that a lot of the time in embedded work you don't get to use the latest version of the standard because you're using a weird or obsolete toolchain that doesn't support it. Or architectures like PIC, where your stack is hardware limited to 8 levels and indirect addressing is inefficient.

I think a lot of the "pure C" people are disguised as electronics engineers - doing PCB design and microcontroller programming together.

by AviewAnew   2017-08-20

Reference Style - All Levels



Above Intermediate

Uncategorized Additional C Programming Books

  • Essential C (Free PDF) - Nick Parlante
  • The new C standard - an annotated reference (Free PDF) - Derek M. Jones