LOGBOOK

HELP

Quiz Entry - updated: 2026.07.10

How do you iterate through an array using pointer arithmetic?

Walk a pointer from the array's start to a "one-past-the-end" pointer, incrementing it each step — p++ moves to the next element automatically.

int arr[] = {10, 20, 30, 40, 50};
// Points to arr[0]
int *p = arr;
// Points one past last element
int *end = arr + 5;

// Method 1: Pointer increment
while (p < end) {
    printf("%d ", *p);
    // Move to next int
    p++;
}

// Method 2: Pointer in for loop
for (int *p = arr; p < end; p++) {
    printf("%d ", *p);
}

// Method 3: Index with pointer
for (int i = 0; i < 5; i++) {
    // Same as arr[i]
    printf("%d ", *(arr + i));
}

Common idiom - process until sentinel:

// For strings (null-terminated)
char *s = "hello";
// Until null terminator
while (*s) {
    // Print and advance
    putchar(*s++);
}

// For NULL-terminated pointer arrays
char *args[] = {"ls", "-l", NULL};
for (char **p = args; *p != NULL; p++) {
    printf("%s\n", *p);
}

From Quiz: REVE1 / C Programming | Updated: Jul 10, 2026