typestar

const on both sides in C

Where the const sits decides whether the pointer or the pointee is frozen.

size_t measure(const char *text) {
    size_t n = 0;
    while (text[n] != '\0') {
        n++;
    }
    return n;
}

void variants(void) {
    char buffer[] = "typestar";
    const char *reader = buffer;
    char *const anchored = buffer;
    const char *const frozen = buffer;

    reader = "elsewhere";
    anchored[0] = 'T';
    (void) measure(reader);
    (void) measure(frozen);
}

How it works

  1. const char *p: the characters are read-only.
  2. char *const p: the pointer cannot be re-aimed.
  3. const char *const p: neither can change.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
331
Tokens
105
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 70 seconds.

Type this snippet

Step 5 of 5 in Pointers, step 5 of 25 in Pointers & memory.

← Previous Next →