Pointer Basics

Introduction to Pointers
  • Introduction

    • Pointer is a variable that contains memory address of another variable
      • Variables vs. Pointer variables
      • Variables contain the values
      • Pointer variables (pointers for short) contain the address of variables that have the values
      • Variable directly references the value
      • Pointer variable indirectly references the value
      • Referencing a value through a pointer is called Indirection
  • Variable and memory allocation

    • Whenever a variable is declared, memory is allocated for the variable according to its type
    • Variables vs. Pointer variables
  • Pointer Declaration

    • A pointer variable is declared with an asterisk before the variable name
    • The type-specifiers determine the kind of variable the pointer variable points to
    • Declaration
      • data-type *pointer-name;
    • Example
      • int *iPtr;
        /*pointer to a variable of type int, means it contains address of a variable of type int*/
  • Pointer operators

    • C provides & and * operators for pointers
    • &: is address operator.
      • Unary operator returns the address of its operand
    • *: Indirection or de-referencing operator
      • Returns the value of the variable to which its operand points.
    • * and & are inverse of each other
  • Pointer demonstration

  • Pointer assignment

    • Pointer variables should be assigned to 0, Null or an address
    • No other constant can be assigned to a pointer variable
    • Pointer variable of a particular data type can, hold only the address of the variable of the same data type
    • Pointer assignment examples
      int a, b, *p = &a, *q = NULL; //Valid
      q = p; //valid - both p and q are pointing to the addr. of a
      b = &a; //invalid - ordinary variable cannot hold address.
      q = a; //invalid - cannot assign value to the pointer variable
  • Pointer Arithmetic

      Pointer addition or subtraction is done in accordance with the associated data type
      • int -> adds sizeof(int) for every increment (=4)
      • char -> adds sizeof(char) for every increment (=1)
      • float -> adds sizeof(float) for every increment (=4)
      • Etc.,
    • All the operations can be done on the value pointed by the pointer
  • Allowed operations on pointers

    • A pointer variable can be assigned the address of an ordinary variable or it can be null pointer
    • A pointer variable can be assigned the value of another pointer variable
    • An integer quantity can be added to or subtracted from a pointer variable
    • One pointer can be subtracted from another pointer variable provided that both are pointing to same array (maybe at different element)
    • Two pointer variables can be compared
  • Illegal operations on pointers

    • Following are the illegal operations on pointer variables
      • Two pointer variables cannot be added
      • Pointer variable cannot be multiplied or divided by a constant
  • Pointer operations example

    • Pointer arithmetic
      int *ptr, i = 5;
      ptr = &i; //let ptr = address of variable i
      ++*ptr; // or (*ptr)++ -> increments value i by 1
      ptr++; //ptr = address of i + 4
    • Pointer operations
      Legal operations
          p1 > p2;
          p1 == p2;
          p1-p2; //If p1, p2 point to the same array
      Illegal operations
          p1 + p2;
  • CPointerDemo Source Code


     * File:   main.c

     * Author: phamvanvung


     * Created on September 21, 2014, 5:57 PM



    #include <stdio.h>

    #include <stdlib.h>





    int main(int argc, char** argv) {

        int x;

        x = 15;

        printf("Address of variable x is %d\n", &x);


        int* xPtr;

        xPtr = &x;//xPtr is pointing to variable x.


        printf("Current value of x is: %d\n", x );

        printf("Access to value of x via its pointer: %d\n", *xPtr);


        printf("Changing the value of x using its pointer to 18\n");

        *xPtr = 18;

        printf("Now the value of x is: %d\n", x);

        return (EXIT_SUCCESS);