Pointers and arrays
A pointer stores an address; an array is a contiguous block of values.
What you will be able to do
- Use size_t for array lengths and loop indexes.
- Dereference only pointers you know are valid.
- Prefer sizeof(array) / sizeof(array[0]) only inside the array’s scope.
In plain English
Pointer arithmetic is measured in elements, not bytes. Arrays decay to pointers when passed to functions, so the length must be passed separately. Never read or write outside the allocated bounds.
#include <stdio.h>
int sum(const int *values, size_t length) {
int total = 0;
for (size_t i = 0; i < length; i++) {
total += values[i];
}
return total;
}
int main(void) {
int scores[] = {8, 7, 9};
printf("%d\n", sum(scores, 3));
return 0;
}Your turn
MediumFind the maximum
Write a function that returns the largest value in an int array plus its length.
C · starter
int maximum(const int *values, size_t length) {
return 0;
}Check your understanding before moving on.
Knowledge checkQuestion 1
Why can a function not determine an array length from int *values?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.