liveBook · Manning

Basic type

As explained in Modern C, Third Edition

A base type is the type from which a derived C type, such as a pointer type, is formed.

Consider `double* b`. Its pointer type is derived from `double`, its base type. Change the base type and the result is a distinct pointer type: `double*` and `size_t*` are not the same type. This distinction matters when a pointer addresses an object; C reasons about the object's type, not its physical address. section 12.0

C's basic types supply starting points for derived types. They include unsigned integers, signed integers, real floating-point numbers, and complex floating-point numbers. Types within a class differ in precision and valid range. Basic types can be specified with language keywords, while headers also provide types organized by purpose, such as `size_t`. section 5.2

TAKEAWAY: Keep the type distinction when reasoning about arithmetic and memory.

  • Narrow integer types are promoted before arithmetic; `char` and `bool` are among them. section 5.2
  • Pointers with distinct base types are distinct pointer types. Except for character types, only pointers of the same base type may alias—access the same object. section 12.0 section 12.3
 size_t blob(size_t const* a, double* b) {
   size_t myA = *a;
   *b = 2*myA;
   return *a;      // Must be myA
 }

In `blob`, the parameters have `size_t` and `double` as their underlying base types. The assignment through `b` therefore cannot change the object read through `a` in a valid call; the returned value remains `myA`. section 12.3

Do not try to defeat that constraint by passing pointers that make differently typed parameters address the same object. Such a call can have undefined behavior. section 12.3

How Modern C, Third Edition covers it

How deep it goes
Classifies basic types by numeric class and precision, then defines pointer base types; defers construction of derived types.
Examples
Contrasts `double const*` with `double*` for possible aliasing and `size_t const*` with `double*` for non-aliasing.
What you can do after
Evaluate/understand it: distinguish pointer types by base type and reason about permitted aliasing.
How it connects
Connects basic types to precision, integer promotions, pointer types, and aliasing.

Also explained in

Compare how 2 books explain basic type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.