Printing and Declaring an Array of Strings


Printing an array of strings: The following program illustrates 7 different ways to print an array of strings:

Printing an array of strings Run of the program
/* Months program */
#include <stdio.h>

void print_months1(char * []);
/* (char **) */ void print_months2(char **); /* (char * []) */ void main(void) { int i, j; char **p; char *q; char *months[] = {"none", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", NULL}; /**** 1st ********************************/ printf("First print of months "); for (i = 1; i <= 12; i++) printf("%s ", months[i]); /**** 2nd ********************************/ printf("Second print of months "); for (i = 1; i <= 12; i++) printf("%s ", *(months + i)); /**** 3rd ********************************/ printf("Third print of months "); p = &months[1]; while (*p != NULL) printf("%s ", *(p++)); /**** 4th ********************************/ printf("Fourth print of months "); for (i = 1; i <= 12; i++) { j = 0; while ((months[i])[j]) /* months[i][j]*/ putchar((months[i])[j++]); putchar(' '); } /**** 5th ********************************/ printf("Fifth print of months "); p = &months[1]; while (*p) { q = *p++; while (*q) putchar(*q++); putchar(' '); } /**** 6th ********************************/ printf("Sixth print of months "); print_months1(months); /**** 7th ********************************/ printf("Seventh print of months "); print_months2(months); } void print_months1(char *months[]) { int i; for (i = 1; i <= 12; i++) printf("%s ", months[i]); } void print_months2(char **months) { char **p; p = &months[1]; while (*p) printf("%s ", *(p++)); }
% cc -o months months.c
% months
First print of months
January
...
December
Second print of months
January
...
December
Third print of months
January
...
December
Fourth print of months
January
...
December
Fifth print of months
January
...
December
Sixth print of months
January
...
December
Seventh print of months
January
...
December
%

      


Notes:
  1. We don't want to print the string months[0], because it is just "none", but we do want to print the strings months[1], months[2], ..., months[12], which are the real months.
  2. So each of months[i] or *(months+i) for i from 1 to 12 inclusive is a string that needs to be printed.


Declaring an array of strings: The program above illustrated how to declare an array of strings with given string values. Below are two additional ways to declare and create an array of strings.

Declaring an array of strings
#include <stdio.h>


int main() {
int i;
char **p;
char *sarr[14];

sarr[0] = "none";
sarr[1] = "Jan"; sarr[2] = "Feb";
sarr[3] = "Mar"; sarr[4] = "Apr";
sarr[5] = "May"; sarr[6] = "Jun";
sarr[7] = "Jul"; sarr[8] = "Aug";
sarr[9] = "Sep"; sarr[10] = "Oct";
sarr[11] = "Nov"; sarr[12] = "Dec";
sarr[13] = NULL;
for (i = 1; i <= 12; i++)
printf("%s ", sarr[i]);
printf(" ");
p = &sarr[1];
while (*p)
printf("%s ", *(p++));
printf(" ");
}
#include <stdio.h>
#include <stdlib.h>

int main() {
int i;
char **p =
(char **)malloc(sizeof(char *)*13);
char **q;
*(p+0) = "none";
*(p+1) = "Jan"; *(p+2) = "Feb";
*(p+3) = "Mar"; *(p+4) = "Apr";
*(p+5) = "May"; *(p+6) = "Jun";
*(p+7) = "Jul"; *(p+8) = "Aug";
*(p+9) = "Sep"; *(p+10) = "Oct";
*(p+11) = "Nov"; *(p+12) = "Dec";
*(p+13) = NULL;
for (i = 1; i <= 12; i++)
printf("%s ", *(p+i));
printf(" ");
q = (p+1);
while (*q)
printf("%s ", *(q++));
printf(" ");
}
Common Output
% cc -o sarray sarray.c
% sarray2
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
      


Created By Dr. Neal Wager : The University of Texas at San Antonio