C Pointer Arithmetic and Array Parameters
C Pointer Arithmetic and Array Parameters
A function needs to sum a caller’s marks without copying the array. It needs both a pointer to elements and a separate count.
Pointer arithmetic advances in units of the pointed-to element type. Valid arithmetic stays within the same array or its one-past position; the one-past pointer may be compared but not dereferenced.
Explain
Pass an array and its length as an explicit function contract.
Trace
Sum only the first two marks without changing the function.
Apply
Explain why a function needs an element count alongside a pointer.
Pointer Bounds: follow the data
Start
A function needs to sum a caller’s marks without copying the array. It needs both a pointer to elements and a separate count.
Operation
Pointer arithmetic advances in units of the pointed-to element type. Valid arithmetic stays within the same array or its one-past position; the one-past pointer may be compared but not dereferenced.
Result
Total: 120
Understand the operation
An array expression usually converts to a pointer to its first element. marks[i] is defined in terms of pointer arithmetic and dereferencing. Supplying count tells the function where its permitted element accesses stop.
const int *marks prevents this function from changing integers through that pointer. It does not mean the caller’s original array can never change elsewhere. The function still requires a valid pointer and count that describe accessible storage.
Read the complete C program
This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.
C17 · complete program
#include <stdio.h>
static int sum(const int *marks, size_t count) {
int total = 0;
for (size_t i = 0; i < count; i++) total += marks[i];
return total;
}
int main(void) {
int marks[] = {40, 35, 45};
printf("Total: %d\n", sum(marks, 3));
return 0;
}
Expected output
Total: 120
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
static int sum(const int *marks, size_t count) {
int total = 0;
for (size_t i = 0; i < count; i++) total += marks[i];
return total;
}
int main(void) {
int marks[] = {40, 35, 45};
printf("Total: %d\n", sum(marks, 3));
return 0;
}
The function receives a pointer to marks[0] and the count three.
Its accesses select indexes zero, one and two. A count of four would falsely claim an additional accessible element.
Follow the worked calculation
Read the starting values
The function receives a pointer to marks[0] and the count three.
Perform the operation
Its accesses select indexes zero, one and two. A count of four would falsely claim an additional accessible element.
Check the observable result
Total: 120
Total: 120
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
static int sum(const int *marks, size_t count) {
int total = 0;
for (size_t i = 0; i < count; i++) total += marks[i];
return total;
}
int main(void) {
int marks[] = {40, 35, 45};
printf("Total: %d\n", sum(marks, 3));
return 0;
}
Sum only the first two marks without changing the function.
Worked answer
The caller supplies the same start pointer with count two. The function visits two elements and returns 75 without touching the third.
C17 · complete program
#include <stdio.h>
static int sum(const int *marks, size_t count) {
int total = 0;
for (size_t i = 0; i < count; i++) total += marks[i];
return total;
}
int main(void) {
int marks[] = {40, 35, 45};
printf("First two: %d\n", sum(marks, 2));
return 0;
}
Expected output
First two: 75
Run in C compiler →Recognize the important boundaries
Subtract pointers only when they refer to positions in the same array under C’s rules, with a result representable in ptrdiff_t.
sizeof a pointer is not the size of the caller’s array. A function must receive its count or another valid bounds contract.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Pointer arithmetic advances in units of the pointed-to element type. Valid arithmetic stays within the same array or its one-past position; the one-past pointer may be compared but not dereferenced.
Continue with grouping named fields.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.