/*Examples of Arrays of Pointers*/ #include #include #define SIZE 5 int main(void){ /* An array of the numbers 0, 1, 2, 3, 4 in English. Adds a null character ('\0') to end of each string. */ char *array[SIZE]={"zero","one","two", "three", "four"}; /* Note that these strings are stored in read-only memory, so we cannot change the value, or we will crash the program. This code will compile, but will crash your program: */ //array[0][0]='X';//Segmentation fault (core dumped) int i = 0; //display the strings and addresses for(i=SIZE-1;i>=0;i--){ printf("array[%i]=\"%s\" (%p), &array[i]=%p\n", i, array[i], array[i], &array[i]); } printf("\n"); //display the characters and addresses int j = 0; int size2 = 0; //size of each string for(i=SIZE-1;i>=0;i--){ //strlen() returns the length of a string (number of characters, except the end of string character) size2 = strlen(array[i]); for(j=size2;j>=0;j--){ if('\0'==array[i][j]){ //output the end of string character ('\0') printf("array[%i][%i]='\\0' (%p)\n", i, j, &array[i][j]); } else{ printf("array[%i][%i]='%c' (%p)\n", i, j, array[i][j], &array[i][j]); } } } printf("\n"); //access a string: char *string2 = array[2]; printf("string2=\"%s\"\n", string2); //access a character char letter2 = array[2][0]; printf("letter2='%c'\n", letter2); return 0; } /* array[4]="four" (0x109d8), &array[i]=0xffbffa40 array[3]="three" (0x109d0), &array[i]=0xffbffa3c array[2]="two" (0x109c8), &array[i]=0xffbffa38 array[1]="one" (0x109c0), &array[i]=0xffbffa34 array[0]="zero" (0x109b8), &array[i]=0xffbffa30 array[4][4]='\0' (0x109dc) array[4][3]='r' (0x109db) array[4][2]='u' (0x109da) array[4][1]='o' (0x109d9) array[4][0]='f' (0x109d8) array[3][5]='\0' (0x109d5) array[3][4]='e' (0x109d4) array[3][3]='e' (0x109d3) array[3][2]='r' (0x109d2) array[3][1]='h' (0x109d1) array[3][0]='t' (0x109d0) array[2][3]='\0' (0x109cb) array[2][2]='o' (0x109ca) array[2][1]='w' (0x109c9) array[2][0]='t' (0x109c8) array[1][3]='\0' (0x109c3) array[1][2]='e' (0x109c2) array[1][1]='n' (0x109c1) array[1][0]='o' (0x109c0) array[0][4]='\0' (0x109bc) array[0][3]='o' (0x109bb) array[0][2]='r' (0x109ba) array[0][1]='e' (0x109b9) array[0][0]='z' (0x109b8) string2="two" letter2='t' */