Pointers, Constants, and Multiple Indirection in C

Pointers can use different levels of indirection; in fact, we often have pointer variables that are pointing to another pointer.

#include <stdio.h>

int main(void){
    
    char *bands[] = {"The Talking Heads", "Elvis Costello", "The Pixies", "Prince"};
    char *albums[] = {"Remain in Light", "This Year's Model", "Doolittle", "1999"};
    
    char **bandsAlbums[2];
    
    bandsAlbums[0] = bands;
    bandsAlbums[1] = albums; 
    
    //loops through first array
    for(int i = 0; i < 4; i++){
        printf("%s\t", *(*bandsAlbums+i));
    }
    
    //first element of second array
    //Remain In Light
    printf("\n%s\t", **(bandsAlbums+1));
    
    //second element of second array
    //This Year's Model
    printf("%s\t", *(*(bandsAlbums+1)+1));
    
    return 0;
    
}


Multiple indirection is most commonly used with strings, since a string is itself an array. However, we can use multiple indirection with any sort of array.

#include <stdio.h>

int main(void){
    
    int values[10];
    int tenValues[10];
    
    for(int i = 0; i < 10; i++){
        values[i] = i+1;
    }
    
    for(int i = 0; i < 10; i++){
        tenValues[i] = (i+1)*10;
    }
    
    int *iPtr[2];
    
    iPtr[0] = values;
    iPtr[1] = tenValues;
    
    //prints 1
    printf("%d\t", **iPtr);
    //prints 10
    printf("%d\t", **(iPtr+1));
    
    //prints 2
    printf("%d\t", *(*iPtr+1));
    //printfs 20
    printf("%d\t", *(*(iPtr+1)+1));
    
    return 0;
    
}

Note that there is not an inherent limit on the number of levels of indirection possible.

Pointers and the const keyword go hand-in-hand in C. A pointer can be defined as a pointer to a constant, which means that the pointer cannot be used to modify the value it is referencing.

#include <stdio.h>

int main(void){
    
    double d01 = 22.41;
    double d02 = 169.254;
    
    double *dPtr;
    const double *const_dPtr;
    
    dPtr = &d01;
    const_dPtr = &d02;
    
    printf("%f\t", *dPtr);
    //can modify value via 
    //regular pointer
    (*dPtr)++;
    printf("%f\t", *dPtr);
            
    printf("\n%f\t", *const_dPtr);
    //we cannot modify value via
    //const pointer
    d02++;
    printf("%f\n", *const_dPtr);
            
    return 0;
    
}

We can dereference a constant pointer, but we cannot change the value to which it is pointing. We can, however, change what the value the pointer is pointing at; the pointer value itself is not constant. The pointer can be changed to point to a different value.

#include <stdio.h>

int main(void){
    

    int a = 42;
    int b = 47;
    
    const int *const_ptr = &a;
    
    
    printf("%d\n", *const_ptr);
    
    const_ptr = &b;
    
    printf("%d\n", *const_ptr);
    
    return 0;
    
}

We can declare a constant pointer to a nonconstant. To do so , we switch the data type and the const keyword. When we do this, it is possible to change the value stored in the variable to which the pointer is pointing; however, we cannot direct the pointer to point to a different variable.

#include <stdio.h>

int main(void){
    
    char a = 'a';
    char b = 'b';
    
    char *const const_ptr1 = &a;
    char *const const_ptr2 = &b;
    
    *const_ptr1+=2;
    *const_ptr2+=2;
    
    //prints c and d, respectively
    printf("%c\n", *const_ptr1);
    printf("%c\n", *const_ptr2);
    
    return 0;
    
    
}

If you can, please purchase a copy of my book. It can be read on your Amazon Kindle, or on an Android tablet or iPad using the Amazon app: http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M/

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s