Consider the following programme:
#include< stdio.h> void main(void) { char name1[16] = "Frans Coenen"; char name2[16] = {'F','r','a','n','s',' ','C','o','e','n','e','n','\0'}; char name3[16] = {70,114,97,110,115,32,67,111,101,110,101,110,0}; int i=0; /* Output */ printf("name1 = %s\n",name1); printf("name2 = %s\n",name2); printf("name3 = %s\n\n",name3); /* Output as sequence of characters. */ while (name1[i] != '\0') { printf("%c",name1[i]); i++; } printf("\n\n"); /* Output as sequence of ASCII codes. */ i=0; while (name1[i] != 0) { printf("%d ",name1[i]); i++; } printf("\n\n"); }
This program includes three arrays, the first is read in using normal string notation, the second as a sequence of individual characters (note that `/0' is the null terminator) and the third as a sequence of ASCII codes (note that 32 is a space and 0 is the null terminator). The three notations are equivalent (although the string notation is the most succinct). This is demonstrated by the three output statements where the three arrays are output as strings (%s) and the following two output loops where name1 is output first as a sequence of characters (%c) and then as a sequence of (ASCII) integers ($d).
The output from the above would be as follows:
name1 = Frans Coenen name2 = Frans Coenen name3 = Frans Coenen Frans Coenen 70 114 97 110 115 32 67 111 101 110 101 110
The definition of a string (or any array for that matter) automatically involves a pointer. Consider the following two definitions:
char name[16];
Both reserves storage for 16 characters and automatically creates a pointer called name which points to the beginning of the array. Consider the following program:
#include< stdio.h> void main(void) { char name[16] = "Frans Coenen"; char *namePtr = "Frans Coenen"; /* Output */ printf("name = %s\n",name); printf("namePtr = %s\n",namePtr); /* Output start location */ printf("Address of first array location for name = %p\n",name); printf("Address of first array location for namePtr = %p\n\n",namePtr); /* New assignments */ strcpy(name,"Frans P. Coenen"); namePtr = "Frans P. Coenen"; /* Output */ printf("name = %s\n",name); printf("namePtr = %s\n",namePtr); printf("Address of first array location = %p\n",name); printf("Address of first array location = %p\n",namePtr); }
Here we have defined two data items; the first is a character array of 16 elements and the other is a pointer to a character. Both have strings assigned to them. The difference is that the first immediately sets aside space for 16 characters while the second creates space on assignment. The character pointer (namePtr) may than be assigned another string later in the code using an assignment operator. This is not the case for the character array (name) which can only be "updated" using a strcpy command.
The output from the above will be:
name1 = Frans Coenen name2 = Frans Coenen Address of first array location = 0xbffffcb8 Address of first array location = 0x804860c name1 = Frans P. Coenen name2 = Frans P. Coenen Address of first array location = 0xbffffcb8 Address of first array location = 0x804867e
Note that the storage address of the first array location for name stays constant while that for the string assigned to namePtr changes on each fresh assignment. This is what distinguishes the two approaches.
Arrays of strings (arrays of character arrays) can be declared and handled in a similar manner to that described for 2-D arrays. Consider the following example:
#include< stdio.h> void main(void) { char names[2][8] = {"Frans", "Coenen"}; /* Output */ printf("names = %s, %s\n",names[0],names[1]); printf("names = %s\n",names); /* Output initials */ printf("Initials = %c. %c.\n",names[0][0],names[1][0]); }
Here we declare a 2-D character array comprising two "roes" and 8 "columns". We then initialise this array with two character strings. The output the array we need to index into each row --- using the 2-D array name on its own (strings) as a pointer cause only the first element ("row") to be produced. Note that we can still index to individual elements using index pairs. The output from the above will be:
names = Frans, Coenen names = Frans Initials = F. C.
The declaration of an array of character pointers is an extremely useful extension to single string pointer declarations.For example:
char *names[2];
Creates a two element array of character pointers (i.e. each element is a pointer to a character). As individual pointers each can be assigned to point to a string using a normal assignment statement:
names[0] = "Frans"; names[1] = "Coenen";
Alternatively we can initialise the names array as shown in the following programs:
#include< stdio.h> void main(void) { char *names[2] = {"Frans", "Coenen"}; /* Output */ printf("names = %s, %s\n",names[0],names[1]); /* New assignments */ names[0] = "Ray"; names[1] = "Paton"; /* Output */ printf("names = %s, %s\n",names[0],names[1]); }
In the above the declaration of names both creates an array of pointers and initilises the pointers with appropriate addresses. Once addresses have been assigned to the pointers, each pointer can be used to access its corresponding string. The output from the above is as follows:
names = Frans, Coenen names = Ray, Paton
Created and maintained by Frans Coenen. Last updated 03 July 2001