Looping through a null-terminated array of strings of unknown size

Sometimes, it is inconvenient to either state or compute the size of an array of some type of objects. That being the case, we must use a termination marker; usually a NULL or 0. For example, suppose we have a fixed array of strings, which we terminate with 0:

char *epics[] = {"AZN", "BOGUS", "ULVR", "VOD", 0} ;

The question is: how do we loop over that array? It can be quite difficult to work out the pointer arithmetic. The answer looks like this:

  char **epic = epics;
  while(*epic) {

In this example, all we are doing is printing the string to stdout. You are likely to want to do something more complicated. This will produce the output:


I hope that helps.

About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s