Exercise 10-8 - CBD page 355 - Lone Leth Thomsen with contributions from Kurt Nørmark

Solution index                   Textual C program


/** Solution to exercise 10.8, p. 355, "C by Dissection"
 *
 *  10. March, 2003. Lone Leth Thomsen
 **/

#include <stdio.h>

int main(int argc, char* argv[])
{
    char* p[2][3] = {{"abc", "defg", "hi"},{"jklmno", "pqrstuvw", "xyz"}};

    printf("%s%c%s","***p                   = ", ***p                    , "\n");     
    printf("%s%c%s","p[0][0][0]             = ", p[0][0][0]              , "\n");     
    printf("%s%c%s","**p[1]                 = ", **p[1]                  , "\n");    
    printf("%s%c%s","p[1][0][0]             = ", p[1][0][0]           , "\n");       
    printf("%s%c%s","**(p[1] + 2)           = ", **(p[1] + 2)            , "\n");    
    printf("%s%c%s","p[1][2][0]             = ", p[1][2][0]            , "\n");     
    //printf("%s%c%s","*(*(p + 1) + 1)[7]   = ", *(*(p + 1) + 1)[7]      , "\n");    
    printf("%s%c%s","(*(*(p + 1) + 1))[7]   = ", (*(*(p + 1) + 1))[7]       , "\n");  
    printf("%s%c%s","p[1][1][7]             = ", p[1][1][7]       , "\n");            
    printf("%s%c%s","*(p[1][2] + 2)         = ", *(p[1][2] + 2)          , "\n");     
    printf("%s%c%s","p[1][2][2]             = ", p[1][2][2]              , "\n");      
						 
return 0;
}





The easiest way to understand what is going on in this exercise is to look at the two dimensional string array, as it is conceived by C at run time. Here is the table.

 

int main(int argc, char* argv[])
This program prints each question from the table and its equivalent. Looking at the result table should make it easier to understand how pointers and indexing works. The lines starting with printf are where the results are printed. The dissection gives the results for each printf line.
 

printf("%s%c%s","***p                   = ", ***p                    , "\n");     
p refers to the first element in the outer table. *p a pointer to the first element in the leftmost inner array. **p is a pointer to the first char in the "abc" array. ***p is the first element in that array, namely 'a'.
 

printf("%s%c%s","p[0][0][0]             = ", p[0][0][0]              , "\n");     
p[0][0][0] is the equivalent expression, evaluating to the value 'a'.
 

printf("%s%c%s","**p[1]                 = ", **p[1]                  , "\n");    
p[1] is the righmost inner array, or more precisely a pointer to the rightmost three element array. *p[1] is a pointer to the "jklmnop" char array. **p[1] is the first element in that array, namely 'j'.
 

printf("%s%c%s","p[1][0][0]             = ", p[1][0][0]           , "\n");       
p[1][0][0] is the equivalent expression, evaluating to the value 'j'.
 

printf("%s%c%s","**(p[1] + 2)           = ", **(p[1] + 2)            , "\n");    
p[1] is the second element in the outer array. More more precisely, a pointer to element 0 in that array. p[1] + 2 is pointer addition, which gives us a pointer to the last element in the three element array. (p[1] + 2)* is a pointer to the first element in the "xyz" array. Finally, **(p[1] + 2) the first char in that string, namely 'x'
 

printf("%s%c%s","p[1][2][0]             = ", p[1][2][0]            , "\n");     
p[1][2][0] is the equivalent expression, evaluating to the value 'x'.
 

printf("%s%c%s","(*(*(p + 1) + 1))[7]   = ", (*(*(p + 1) + 1))[7]       , "\n");  
p + 1 is a pointer to the second element in the outer array. *(p + 1) is a reference to the three element array in that cell - or more precisely a pointer to the first element in that array. (*(p + 1) + 1) displaces this pointer to point to the second element in that array. (*(*(p + 1) + 1)) gives a reference to the first element in the "pqrstuvw" array. Finally (*(*(p + 1) + 1))[7] returns the seventh char in the "pqrstuvw" array, namely 'w'.
 

//printf("%s%c%s","*(*(p + 1) + 1)[7]   = ", *(*(p + 1) + 1)[7]      , "\n");    
This line gives an error since the expression evaluates to something that does not make sense. The reason is that index [7] is taken before the last dereferencing.
 

printf("%s%c%s","p[1][1][7]             = ", p[1][1][7]       , "\n");            
p[1][1][7] is the equivalent expression, evaluating to the value 'w'.
 

printf("%s%c%s","*(p[1][2] + 2)         = ", *(p[1][2] + 2)          , "\n");     
p[1][2] is the a pointer to the "xyz" array. The addition of 2 in (p[1][2] + 2) displaces the pointer to point at the 'z' char. *(p[1][2] + 2) returns 'z'.
 

printf("%s%c%s","p[1][2][2]             = ", p[1][2][2]              , "\n");      
p[1][2][2] is the equivalent expression, evaluating to the value 'z'.
 


Generated: Wednesday, March 29, 2006, 12:33:07
This program dissection page is generated from an XML-in-LAML source file