Pointers By Example in C

Pointer variables are variables that contain addresses of other variables; they contain the location of regular data variables, in effect pointing to the data because they hold that data’s location.

Pointers offer a powerful and efficient means of accessing and changing data.

Pointers follow all the normal naming rules of regular variables. We must define a pointer before we use it; there are character pointers, floating-point pointers, integer pointers, and so on.

There are two pointer operators in C, the address of operator, &, and the dereferencing operator, *. These operators are overloaded operators, since they can be used for other things, such as multiplication.  When used in the context of pointers, the address of operator always produces the memory address of whatever variable it is placed in front of.

#include <stdio.h>


int main(void){

    int a = 9;
    int b = 10;
    char c = 's';
    double d = 19.99;

    printf("%d located at %p\n", a, &a);
    printf("%d located at %p\n", b, &b);
    printf("%c located at %p\n", c, &c);
    printf("%f located at %p\n", d, &d);

    return 0;    

}

Note that the %p conversion specifier is used to print memory addresses.

To define a pointer variable, we follow the same recipe for defining a variable of the desired type, except we play a * between the type name and the variable’s identifier. Because of the addition of the dereferencing operator, the C compiler knows that we wish to define a pointer. A pointer variable must point to a variable of the same type.

#include <stdio.h>


int main(void){

    double *dPtr;
    int *iPtr;

    int i= 73;
    double d = 25.1;

    //assign memory addresses
    //to the pointers
    dPtr = &d;
    iPtr = &i;

    printf("dPtr = %p\n", dPtr);
    printf("iPtr = %p\n", iPtr);
    
    //rValue of dPtr
    //is equal to the lValue of d
    if(dPtr == &d){
        printf("dPtr == &d\n");
    }    

    return 0;

}

Note that C does not initialize pointers when we define them. We must explicitly assign the pointer variable the memory address of another variable of the same type. To print the value stored by the variable the pointer is pointer to, we use the dereferencing operator, *.

#include <stdio.h>

int main(void){

    char c = 's';

    char *cPtr = &c;

    printf("c = %c\n", c);
    printf("&c = %p\n", &c);
    printf("cPtr = %p\n", cPtr);
    printf("*cPtr = %c\n", *cPtr);    

    return 0;

}

A pointer must be of the same type as the variable its pointing to,  but, there is an exception to this rule. A pointer variable of type void can be used to point to variables of different types, however, we must cast it to the correct data type when we dereference it.

#include <stdio.h>

int main(void){

    int i = 404;
    unsigned uInt = 404405;
    double dare = 169.254;

    void * vPtr = &i;
    printf("%d\t", *(int *)vPtr);
    
    vPtr = &uInt;
    printf("%u\t", *(unsigned *)vPtr);

    vPtr = &dare;
    printf("%f\n", *(double *)vPtr);


    return 0;

}

To pass a variable to a function by reference instead of by value, place an & in front of the variable in the argument list, and an * everywhere it appears in the function body.

#include <stdio.h>

void incrementValue(int *i);

int main(void){

    int i = 46;
    int j = 73;

    printf("i = %d\t", i);

    incrementValue(&i);

    printf("now i = %d\n", i);

    printf("i = %d, j = %d", i, j);    

    return 0;

}


void incrementValue(int *i){
    *i+=2;
}

Our next program will swap the values of two integers using a function that accepts its arguments by reference.

#include <stdio.h>

void swapInts(int *a, int *b);

int main(void){

    int i = 42;
    int j = 73;

    printf("i = %d, j = %d\n", i, j);

    swapInts(&i, &j);


    printf("i = %d, j = %d\n", i, j);

    return 0;

}


void swapInts(int *a, int *b){
    int swap = *a;
    *a = *b;
    *b = swap;
}

Finally, it is possible to construct an array of pointers, with each pointer being a pointer to a specific data type.

#include <stdio.h>

int main(void){


    int i, j, k, l, m;

    int *intPtrs[5];

    intPtrs[0] = &i;
    intPtrs[1] = &j;
    intPtrs[2] = &k;
    intPtrs[3] = &l;
    intPtrs[4] = &m;

    for(int counter = 0; counter < 5; counter++){
        *intPtrs[counter] = (counter + 1) *3;
    }

    printf("%d \n", k);
    printf("%d \n", m);

    return 0;

}

If you can, please take a look at my Amazon.com author page: http://www.amazon.com/Al-Jensen/e/B008MN382O/

 

 

 

 

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