Pointers in C programming

Pointer Variable Definitions and Initialization

Pointers are variables whose values are memory addresses. Normally, a variable directly contains a specific value.

A pointer, on the other hand, contains an address of a variable that contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value. Referencing a value through a pointer is called indirection.

Declaring Pointers, like all variables, must be defined before they can be used.

Pointers in C
Pointers in C programming
int *countPtr, count;

Pointer Operators

The &, or address operator, is a unary operator that returns the address of its operand.

    //the definition
    int y = 5;
    int *yPtr;
    //the statement assigns the address of 
    the variable y to pointer variable yPtr.
    yPtr = &y;

Variable yPtr is then said to “point to” y.

The unary * operator, commonly referred to as the indirection operator or dereferencing operator , returns the value of the object to which its operand points.

    printf( "%d", *yPtr );

The statement prints the value of variable y, namely 5. Using * in this manner is called dereferencing a pointer .

Demonstrating the & and * Operators

    // Using the & and * pointer operators.
    #include <stdio.h>
    int main( void ) {
    int a; // a is an integer
    int *aPtr; // aPtr is a pointer to an integer
    a = 7;
    aPtr = &a; // set aPtr to the address of a
    printf( "The address of a is %p"
           "\nThe value of aPtr is %p", &a, aPtr );
    printf( "\n\nThe value of a is %d"   
           "\nThe value of *aPtr is %d", a, *aPtr );
    printf( "\n\nShowing that * and & are complements of "
           "each other\n&*aPtr = %p"   
           "\n*&aPtr = %p\n", &*aPtr, *&aPtr );
    } // end main 

    The address of a is 0029FF0C
    The value of aPtr is 0029FF0C
    The value of a is 7
    The value of *aPtr is 7
    Showing that * and & are complements of each other
    &*aPtr = 0029FF0C
    *&aPtr = 0029FF0C