Introduction to C Pointers: Basic

 

Introduction

In this article I'll cover the most irritating stuff of the C Programming language, pointers. Pointers are very important for utilizing the power of C. Most of the high-level languages that have come after C/C++ promote managed code and avoid the use of pointers. But still the world's most popular languages are C and C++. Both of these languages rely extensively on pointers. So let's start!

Understanding Pointers

In C we have variables. Variables are placeholders for values. The values are stored in main memory. Memory can be conceptualized as a set of blocks. Each block is associated with a unique address. Now to store any values we have ordinary variables. But when we want the address of that ordinary variable in memory then we need a pointer. In other words variables store a value and a pointer stores the address of that variable. The address of the variable is stored in a pointer. Simple ? Not yet, see the following image.

 

Pointer Syntax

The syntax is very simple. To declare a pointer we need to know two things, first is the data type of the pointer and the second is whether it's a constant pointer or not. For now let's focus on the first thing only.

To declare a pointer, the generic syntax is:

data_type* pointer_name;
or
data_type *pointer_name;

Both declarations look the same and they are. So you can choose any of these two but be consistent throughout the code.

Examples:

  1. int *myPtr for a pointer to an integer.
  2. float *myPtr for a pointer to a float.
  3. char *myPtr for a pointer to a character.

The default value of a pointer is undefined, in other words it can be anything and is unpredictable. Pointer data types must be very specific for holding a specific data type variable.

Accessing Pointer Value

A pointer contains an address of a memory location where the value is stored. To access that value we use the "value of" operator or dereferencing operator. When this "*" is in a declaration then it's a part of a pointer declaration syntax. When this "*" precedes a pointer name then its the deference operator. The process of obtianing the value that a pointer ponts to is known as a pointer dereference.

 Example:

  1. int myVar= *myPtr; Retrieves the value pointed by myPtr and assigns it to myVar

Dereferencing an uninitilized variable causes an undefined behavior. Also an address of operator and dereference operator cancel out each other as in the following:

           int *myPtr1=&(*&var1)
is the same as:
           int *myPtr1=&var1
;

 

Summary

  1. Address Of operator (&)
    It is used for extracting the address of the variable.

    Op:
    Address of var1 0xbfc20e3c 
    Address of var1 using pointer 0xbfc20e3c  
  2. Pointer assignment
    Only compatible pointers are assignable to each other.
     


    OP
    Address of var1 0xbfc20e3c
    Address of var1 using pointer 0xbfc20e3c
  3. Pointer dereference
    To extract the value of a pointer:



    OP
    Value of var1 using value of(*) operator on myPtr1 pointer is 4 
  4. Example
     

    #include <stdio.h>

    int main(void){

                    int var1=4;

                    int var2=5;

                    int*myPtr1=&var1;

                    int*myPtr2=&var2;

                    printf("Value of var1 is %d at address %p",*myPtr1,myPtr1);

                    printf("\nValue of var2 is %d at address %p",*myPtr2,myPtr2);

                    printf("\nAssigning myPtr1 to myPtr2");

                    myPtr2=myPtr1;

                    printf("\nValue of myPtr2 after pointer assignment is %d",*myPtr2);

    printf("\nAddressed of myPtr1 and myPtr2 is equal after pointer assignment ?:%s \n",(myPtr1==myPtr2)?"True!":"False");

                    return0;

    }


     

    Output

    Value of var1 is 4 at address 0xbfabac58
    Value of var2 is 5 at address 0xbfabac5c
    Assigning myPtr1 to myPtr2
    Value of myPtr2 after pointer assignment is 4
    Addressed of myPtr1 and myPtr2 is equal after pointer assignment:True! 

Up Next
    Ebook Download
    View all
    Learn
    View all