Skip to content
Learn Netverks

Lesson

Step 15/36 42% through track

pointer-arithmetic

Pointer arithmetic

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Pointer arithmetic: the syntax, patterns, and safety habits you need before advancing in C.

Pointers and dynamic allocation are the heart of C interviews—segfaults and leaks come from misunderstanding ownership.

You will apply Pointer arithmetic in contexts like: Custom allocators, linked structures, and hand-off APIs in systems libraries.

Write C in main.c with int main(), click Run on server—the dev runner compiles with cc/gcc -std=c11 and runs the binary; read stderr for compile and linker errors (LEARNING_RUNNER_ENABLED=true). Also never return addresses of local variables from functions.

When you can explain the previous lesson's ideas without copying starter code.

Adding an integer to a pointer moves by sizeof(element) bytes, not one byte. This enables efficient array iteration without index syntax.

Walking an array

int data[] = {1, 2, 3};
int *p = data;
for (int i = 0; i < 3; i++) {
    printf("%d\n", *(p + i));
}

Pointer differences

end - start yields the number of elements between two pointers into the same array—undefined if they do not point into the same object.

Important interview questions and answers

  1. Q: p + 1 for int*?
    A: Advances by sizeof(int) bytes to the next int, not the next byte.
  2. Q: Arithmetic on void*?
    A: Not allowed in standard C—cast to a typed pointer first.

Self-check

  1. Are p[i] and *(p+i) equivalent?
  2. Why must pointer subtraction stay within one array?

Interview prep

p + 1 for int*?

Advances by sizeof(int) bytes, not one byte—scale depends on pointed-to type.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Valid when?
  • Off-by-one bug?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump