Dayrvia
C

C

Trace addresses and values, array traversal, double pointers, const and object lifetimes.

Take the full quiz

Variables and operatorsInteger division

Dividing integers

When both operands are integers, division discards the fractional part. Storing the result in a floating-point variable afterwards cannot recover it.

Code example

double a = 5 / 2;
double b = 5.0 / 2;

Walkthrough

a holds 2.0 and b holds 2.5. The second expression uses a floating-point operand before division.

ArraysIndexing and loops

Walking an array

A three-element array has indices 0, 1 and 2. Identify the starting index, loop condition and step before tracing each iteration.

Code example

int a[] = {2, 4, 6};
int sum = 0;
for (int i = 0; i < 3; i++) sum += a[i];

Walkthrough

At indices 0, 1 and 2, sum becomes 2, 6 and 12. The loop stops as soon as i reaches 3.

PointersAddresses and dereferencing

Changing a value through its address

The & operator takes an address and * accesses the object at that address. A function receives a copy of the pointer but can still change the original object.

Code example

void bump(int *p) { *p += 1; }
// inside main
int n = 4;
bump(&n);

Walkthrough

bump receives the address of n. Adding one through *p leaves n equal to 5.

PointersPointer arithmetic

Pointer arithmetic counts elements

Adding one to an int pointer moves it by one int element. Mark its starting index, add the offset, then read the value where the dereference operator appears.

Code example

int a[] = {4, 7, 11, 15};
int *p = &a[1];
int value = *(p + 2);

Walkthrough

p starts at index 1. Adding 2 identifies index 3, so value becomes 15. The expression does not change p itself.

PointersIncrement operators

Increment the value or the pointer?

(*p)++ increments the integer at p. In *p++, postfix ++ binds first: *(p++) reads at the old position and advances the pointer. Track the array and pointer separately after each statement.

Code example

int a[] = {4, 9};
int *p = a;
(*p)++;
int first = *p++;

Walkthrough

The first change leaves {5, 9}. The next line stores 5 in first and advances p to a[1]. A subsequent read of *p gives 9.

ArraysArray length

Why pass the length as well?

At the declaration site, sizeof a measures the whole array. An array parameter is adjusted to a pointer, which carries no element count. Calculate the count at the caller and pass it alongside the pointer.

Code example

int sum(const int *items, size_t n) {
    int total = 0;
    for (size_t i = 0; i < n; i++) total += items[i];
    return total;
}
// inside main, with <stddef.h>
int a[] = {3, 5, 8};
int result = sum(a, sizeof a / sizeof a[0]);

Walkthrough

The caller divides the array size by the element size to pass 3. sum adds indices 0, 1 and 2 and returns 16.

PointersPointer constness

Which part is const?

A const int * can point to a different integer but cannot write the integer through that pointer. An int *const has a fixed address and can write to a mutable integer there.

Code example

int a = 2, b = 7;
const int *view = &a;
view = &b;
int *const fixed = &a;
*fixed = 4;

Walkthrough

Reading through view gives b, which is 7. fixed continues to point to a, whose value becomes 4.

PointersDouble pointers

Redirecting the caller’s pointer

Assigning a new address to a pointer parameter changes only the local copy. To change the caller’s pointer variable, pass its address and assign the new target through *slot.

Code example

void point_to(int **slot, int *target) {
    *slot = target;
}
// inside main
int a = 3, b = 8;
int *p = &a;
point_to(&p, &b);
*p += 1;

Walkthrough

slot points to p. After the call, p points to b, so the last line changes b to 9. a remains 3.

MemoryObject lifetime

Checking the lifetime of an object

An automatic local variable expires when its block ends. To keep a result, return a value or use storage with a suitable lifetime. With dynamic storage, check allocation and release it once after its final use.

Code example

#include <stdlib.h>

int *make_count(int value) {
    int *count = malloc(sizeof *count);
    if (count != NULL) *count = value;
    return count;
}
// inside main
int *count = make_count(6);
if (count != NULL) {
    int saved = *count;
    free(count);
    count = NULL;
}

Walkthrough

On success, the code copies 6 into saved before releasing the allocation. Setting count to NULL clears only that variable. Other aliases must no longer read the released object.

Further reading