C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

Arrays of Pointers


If you want to use a bunch of pointers, create an array of them. An array of pointers is just as easy to
define as an array of any other kind of data, except that you must include the * operator after the data
type name. The following statements reserve an array of 25 integer pointers and an array of 25
character pointers:


Click here to view code image


int * ipara[25]; /* 25 pointers to integers */
char * cpara[25]; /* 25 pointers to characters */

The array of characters is most interesting because you can store a list of strings in the array. More
accurately, you can point to various strings. The following program illustrates two things: how to
initialize an array of strings at definition time and how to print them using a for loop:


Note

Actually, the program does a bit more than that. It also gets you to rate the nine strings
(in this case, movie titles) that you’ve seen on a scale of 1 to 10 and then reuses our
friendly bubble sort routine—but instead of going small to big, the sort reorders your
list from highest rating to lowest. There’s nothing wrong with going back and mixing in
previously learned concepts when trying new lessons—that’s how you start to build
robust and interesting programs!

Click here to view code image


// Example program #1 from Chapter 25 of Absolute Beginner's Guide
// to C, 3rd Edition
// File Chapter25ex1.c
/* This program declares and initializes an array of character
pointers and then asks for ratings associated */
#include <stdio.h>

main()
{
int i;
int ctr = 0;
char ans;
//Declaring our array of 9 characters and then initializing them
char * movies[9] = {"Amour", "Argo",
"Beasts of the Southern Wild",
"Django Unchained",
"Les Miserables",
"Life of Pi", "Lincoln",
Free download pdf