Video Coming Soon...
13: For-Loops and Arrays of Strings
You can make an array of various types. You can also know that a string and an array of bytes are the same thing. The next step is to do an array that has strings in it. We'll also introduce your first looping construct, the for-loop, to help print out this new data structure.
The fun part of this is that there's been an array of strings hiding in your programs for a while now: the char *argv[] in the main function arguments. Here's code that will print out any command line arguments you pass it:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i = 0;
// go through each string in argv
// why am I skipping argv[0]?
for (i = 1; i < argc; i++) {
printf("arg %d: %s\n", i, argv[i]);
}
// let's make our own array of strings
char *states[] = {
"California", "Oregon",
"Washington", "Texas"
};
int num_states = 4;
for (i = 0; i < num_states; i++) {
printf("state %d: %s\n", i, states[i]);
}
return 0;
}
Register for From Impostor to Programmer
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.