C Replace an array entry
Replace one stop at a known position while preserving the rest of the route.
A simple example
Replace an array entry
- Start with A, B
- Do this Replace the second item with C
- Result A, C
Syntax
stops[index] = destination
Run these examples in C
Save one example at a time as lesson.c. Use C17 and a Unix-style shell.
cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lessonCorrect the second stop on a shuttle route
A dispatcher spots an outdated address at the second stop on today's shuttle route. The position on the schedule is correct, so only the destination at that position should change.
#include <stdio.h>
int main(void) {
const char *stops[] = {"School", "Park"};
stops[1] = "Harbor";
puts(stops[1]);
return 0;
}
Harbor
The second stop changes from Park to Harbor.
Correct a lab register
A student signed the lab sheet with a nickname. The second entry must be corrected in place. Which value occupies that position after the change?
Change the next stop
The shuttle cannot use the quay this morning. The second entry must be corrected in place. Which value occupies that position after the change?
What to watch for
These entries point to string literals. Change the pointer stored in the array and leave the literal text untouched.