YOUR C LEARNING COMPANION

Find your next idea.

Learning path
Correctness & careChapter 60 of 73

Effective types, aliases, and access

Connect permitted access types, representation copies, allocated storage, alignment, and restrict contracts.

6 min readAdvancedC17 baseline
Offline reading
In this chapter8 sections

Having enough bytes at an address does not make every typed access valid. The object’s type, alignment, lifetime, and the access rules still matter. This chapter explains why a cast cannot make arbitrary storage safe to read through any pointer.

1. Multiple valid views can refer to one object

C17 · illustrative fragment
int value = 7;
int *writer = &value;
const int *reader = &value;
*writer = 9;

Both pointers designate the same int object. A const-qualified view permits reading that compatible type without allowing modification through that view. It does not freeze all other valid access paths. This is ordinary aliasing, not a violation. An alias also does not extend an object’s lifetime after free or scope exit.

2. Effective type governs the allowed stored-value access

For an object with a declared type, its effective type is that declared type. C17 permits stored-value access through a compatible type, appropriate qualified versions, corresponding signed/unsigned types, certain aggregates/unions containing such a type, and a character type. This is a structured list of permissions, not a rule that every pointer cast makes a new type valid.

For example, casting an int pointer to a float pointer and reading through it does not become valid merely because their sizes happen to match. Misalignment may already invalidate the conversion, and suitable alignment still does not grant a permitted type access. The earlier union lesson explains C’s separate direct union-member rules; those are not a general license for unrelated pointer punning.

3. Character access gives a representation view

C17 · illustrative fragment
int original = 73;
unsigned char bytes[sizeof original];
memcpy(bytes, &original, sizeof bytes);
int restored = 0;
memcpy(&restored, bytes, sizeof restored);

The object representation can be copied into an unsigned-char array and copied back into a suitable object of the original type. The example recovers the original int; it does not assign a portable byte order or external file format to those bytes. C’s character-type permission includes char, signed char, and unsigned char, although unsigned char is the usual representation-view choice.

The permission is asymmetric: reading an int’s representation through character access does not mean that an arbitrary declared char array may be read through int *. Adding alignment to that array alone does not change its declared type. Reading arbitrary bytes as a typed value can also produce a trap representation on implementations/types that have one.

4. Allocated storage has no declared type of its own

Complete C17 program
#include <assert.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int original = 73;
    void *storage = malloc(sizeof original);
    if (storage == NULL) return EXIT_FAILURE;
    memcpy(storage, &original, sizeof original);
    const int *view = storage;
    assert(*view == 73);
    free(storage);
    return 0;
}

An allocation does not acquire an int type simply because the receiving pointer variable is int *. A modifying store through a non-character lvalue establishes the effective type for that access and subsequent nonmodifying accesses under C17’s rule. Copying from an object with memcpy/memmove can carry the source’s effective type into allocated storage for those later accesses, as shown here. Other accesses have their own effective-type rule; do not reduce this to a permanent unchangeable type tag.

The allocation must still be large enough and suitably aligned for the intended object. malloc supplies the required fundamental alignment for this int example. Copying bytes into an already declared array does not erase that array’s declared type. Keep allocated storage, declared objects, and external byte encodings conceptually separate.

5. Restrict is an access promise with a scope

Complete C17 program
#include <assert.h>
#include <stddef.h>

static void copy_values(int *restrict destination, const int *restrict source, size_t count)
{
    for (size_t i = 0; i < count; ++i) destination[i] = source[i];
}

int main(void)
{
    const int source[] = { 2, 4, 6 };
    int destination[3];
    copy_values(destination, source, 3);
    assert(destination[0] == 2 && destination[2] == 6);
    return 0;
}

restrict adds an execution-time access contract, not a runtime overlap test. The separate arrays satisfy this helper’s intended contract. Reading through source while modifying the same overlapping elements through the independent restricted destination can violate that contract. For the objects modified in the relevant block execution, accesses must follow the permitted restricted pointer family.

Do not translate restrict as “all pointer values must be different.” Read-only accesses, derived pointers, block structure, and which objects are modified matter. If overlapping movement is needed, choose an interface designed for it, such as memmove for appropriate byte regions, and meet that interface’s contracts. Compiler aliasing options and extensions need separate documentation; they do not rewrite ISO C’s portable access rules.

Primary references and further reading

Before you move on

  • Typed access depends on the object, its lifetime, alignment, and the permitted access type.
  • A character representation view does not grant arbitrary typed reinterpretation.
  • restrict is a contract about the relevant accesses, not a generic synonym for “these pointers differ.”
Keep these ideas separate
Pointer cast / Valid access
A cast changes the expression’s type without automatically establishing alignment, lifetime, or effective-type requirements.
Explain it in your own words

Why can a buffer with enough bytes still be unsuitable for access through a converted typed pointer?

Optional: say it aloud or keep a note. You can continue whenever you are ready.

Check your understanding

Optional. Answers are not saved or sent, and reading stays unrestricted.

Why is alignment alone insufficient to justify a dereference after a pointer cast?
Read the explanation for every choice
  1. A cast automatically creates a new object of the destination type.

    Changing a pointer’s type does not generally create a new pointed-to object.

  2. A cast makes every possible access type valid.

    The language restricts which types may access an object’s stored value.

  3. Lifetime, access-type rules, and bounds must also permit the operation.

    ✓ Correct reasoning. Character-type representation access does not grant unrestricted permission for arbitrary typed reads.

C17

Ready for the next idea?

Mark this chapter as read whenever you feel ready.

CHAPTER 60 / 73

In this chapter

Effective types, aliases, and access

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping