Exercise 13. For-Loops and Arrays of Strings

You can make an array of various types with the idea 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:

ex13.c

 1  #include <stdio.h>  2  3  int main(int argc, char *argv[])  4  {  5      int i = 0;  6  7      // go through each string in argv  8      // why am I skipping argv[0]?

Get Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.