|< C & C++ Arrays 2 | Main | C & C++ Pointers 2 >|Site Index |Download |


 

 

 

 

 

MODULE 8

C/C++ POINTERS - A DERIVED DATA TYPE 1

Point to here, point to there, point to that, point to this, and point to nothing!
well, they are just memory addresses!!

 

 

 

 

 

 

My Training Period: xx hours

 

This Module may be one of the toughest topics in C / C++ that complained by students :o), although it is one of the most important topic. The source code for this module is available atC/C++ pointers program source codes. The lab worksheets for your practice are:C/C++ pointers part 1 andC/C++ pointers part 2. Also the the related exercises in theIndirection Operator lab worksheet 1,lab worksheet 2 and labworksheet 3.

 

The C and C++ pointers programming abilities that should be acquired:

 

 

 

 

 

 

 

 

 

8.1      What Is Pointers?

  • Pointers provide a powerful and flexible method for manipulating data in programs.

  • Some program routines can be more efficiently executed with pointers than without them, and some routines can be performed only with pointers.

  • To understand how to use pointers, a programmer must have a basic knowledge of how a computer stores information in memory.  Pointers closely related to memory manipulation.

  • Basically, a personal computer Random Access Memory (RAM) consists thousands of sequential storage locations, with each location being identified by a unique address.  Computer’s processor also has their own memory, normally called registers and cache.  They differ in term of access speed, price and their usages.

  • The memory addresses in a given computer, range from 0 to a maximum value that depends on the amount of physical memory installed.

  • Nowadays 128, 256 MB up to GB of RAM installed on the PCs are normal.

  • Computer memory is used for storage, when program runs on the computer and for processing data and instructions.

  • For example, when you use Microsoft Word program, it will occupy some of the computer’s memory.

  • In a very simple way, each program requires two portions of the memory that is:

  1. Data portion – for data or operands.

  2. The instruction code portion – what to do to the data such as operators (add, divide, move etc.)

  • Each portion is referred to as a memory segment, so there is:

  1. A data segment (normally called data segment).

  2. An instruction code segment (normally called text segment).

  • Now we just concentrate the memory storage for program data, the data segment.  Details of the memory allocation story can be found inCompiler, Assembler & Linker andC Storage and Memory Allocation.

  • When the programmer declares a variable in a C/C++ program, the compiler sets aside a memory location with a unique address to store that variable.

  • The compiler associates that address with the variable’s name.  When the program uses the variable name, it automatically accesses a proper memory location.

  • The locations address remains hidden from the programmer, and the programmer need not be concerned with it.  What we are going to do is to manipulate the memory addresses by using pointers.

  • Let say we declare one variable named rate of type integer and assign an initial value as follows:

int   rate = 100;

C/C++ pointer and memory address

Figure 8.1

C/C++ pointer and memory address illustration

Figure 8.2

C/C++ pointer and memory address

Figure 8.3

C/C++ pointer and memory address

Figure 8.4

int   *s_rate;

8.2    Pointers And Simple Variables

data_type    *pointer_variable_name;

char*    x;

int   *    type_of_car;

float    *value;

// ch1 and ch2 both are pointers to type char.

char    *ch1, *ch2;

 

// value is a pointer to type float, and percent is an ordinary float variable.

float    *value, percent;

 

8.3    Initializing Pointers

  1. Indirection operator (*) – has been explained previously.

  2. Address-of-operator (&) – means return the address of.

#include <stdio.h>

 

int main(void)

{

int    *m;

int location = 200;

m = &location;

printf("The data, *m = %d\n",*m);

printf("The address where the data pointed to, m = %d\n", m);

return 0;

}

 

Output:

 

C/C++ pointer indirection and address of operators

 

  • Therefore a pointer must be initialized with a statement that uses& operator as shown below:

// declare a pointer variable, m of type int

int    *m;

// assign the address of variable location

// to variable m, so pointer m is pointing to variablelocation

m = &location;

// the actual data assigned to variable location

location = 200;

  • This statement means places the memory address of variablelocation into variablem.  The memory address refers to the computer’s internal location where the actual data is stored.  What and where the address is? It is determined by the system.

  • In other word, pointer variable m receives the address of variable location or the memory address of the variable location is assigned to pointer variable m.

  • Graphically it can be represented as shown below.

C C++ pointer and memory address

Figure 8.5

int    *m;

m = &location;

location = 200;

q = *m;

  1. When declaring a pointer variable.

  2. When dereferencing a pointer variable (to find the data it points to).

8.4    Using Pointers

// program to illustrate the basic use of pointers

#include <iostream>

usingnamespace std;

 

void main()

{

   // declares an integer variable and two pointers variables

   int    num = 10, *point_one, *point_two;

   // assigns the address of variable num to pointer point_one

   point_one = &num;

    // assigns the (address) point_one to point_two

    point_two = point_one;

    cout<<"Pointers variables..."<<endl;

    cout<<"*point_one = "<<*point_one<<"\n";

    cout<<"*point_two = "<<*point_two<<"\n";

    cout<<"\nNormal variable..."<<endl;

    cout<<"num = "<<num<<"\n";

    // displays value 10 stored in num since point_one

    // and point_two now point to variable num

    cout<<"\n-Both pointer point_one and"<<"\n";

    cout<<"-point_two point to the same variable num."<<"\n";

   cout<<"-That is why, they have same value, 10."<<endl;

}

 

Output:

 

C/C++ pointer program example output snapshot

C C++ pointer and memory address

 

Figure 8.6

  1. Access the contents of a variable by using the variable name (num) and is called direct access.

  2. Access the contents of a variable by using a pointer to the variable (*point_one or *point_two) and is called indirect access or indirection.

// declare a pointer variable named pter, where the

// data stored pointed to by pter is int type

int    *pter;

// assign the address of variable named var to a pointer variable named pter

pter = &var;

  1. *pter and var both refer to the contents of var (that is, whatever data value stored there).

  2. pter and &var refer to the address of var (thepter only hold the address of variablevar not the actual data value).

// a basic pointer use

#include <stdio.h>

 

void main()

{

       // declare and initialize an int variable

       int  var = 34;

       // declare a pointer to int variable

       int  *ptr;

       // initialize ptr to point to variable var

       ptr = &var;

       // access var directly and indirectly

       printf("\nDirect access, variable var value = var = %d", var);

       // you can use %p for the pointer memory address directly or

       //%0xor%0X in hexadecimal representative instead of

       // %d, just to avoid confusion here…

       printf("\nIndirect access, variable var value = *ptr = %d", *ptr);

       // display the address of var two ways

       printf("\n\nThe memory address of variable var = &var = %d", &var);

       printf("\nThe memory address of variable var = ptr = %d\n", ptr);

}

 

Output:

 

C/C++ pointer program example

int    age = 25;

int    *ptr_age;

ptr_age = &age;

ptr_age++;

int = 2 byte.

float = 4 byte.

int        vint = 12252;

char    vchar = 90;

float    vfloat = 1200.156004;

C C++ pointer size of memory address and data type

Figure 8.7

 

  1. int variables occupy 2 byte starts at1000.

  2. char variables occupy 1 byte starts at1003.

  3. float variables occupy 4 byte starts at1006.

int        *p_vint;

char     *p_vchar;

float     *p_vfloat;

p_vint  =  &vint;

p_vchar  =  &vchar;

p_vfloat  =  &vfloat;

p_vint   equals   1000.

p_vchar    equals    1003.

p_vfloat    equals    1006.

ptr1 – ptr2;

 

8.5    Pointers Comparison

==, !=, >, <, >=,and<=

ptr1 < ptr2  is TRUE

Operation

Description

1. Assignment (=)

You can assign a value to a pointer.  The value should be an address with the address-of-operator (&) or from a pointer constant (array name)

2. Indirection (*)

The indirection operator (*) gives the value stored in the pointed to location.

3. Address of (&)

You can use the address-of operator to find the address of a pointer, so you can use pointers to pointers.

4. Incrementing

You can add an integer to a pointer to point to a different memory location.

5. Differencing

You can subtract an integer from a pointer to point to a different memory location.

6. Comparison

Valid only with 2 pointers that point to the same array.

 

Table 8.1:  Pointer operations

 

8.6    Uninitialized Pointers

int    *ptr;

// this is not an address lol!

*ptr = 12;

char    *    mystring = "";

#include <stdio.h>

 

int main()

{

    int    *thepointer = NULL;

   // do some testing....

    printf("The thepointer pointer is pointing to = %X\n", thepointer);

    printf("The thepointer pointer is pointing to = %d\n", thepointer);

    return 0;

}

 

Output:

 

C C++ NULL pointer program example

 

C & C++ programming tutorials

 

 

 

 

 

 

 

Further related C and C++ pointers reading and digging:

 

  1. Check the best selling C / C++ books at Amazon.com.
  2. The source code for this module is: C/C++ pointers program source codes.
  3. The lab worksheets for your practice are:C/C++ pointers part 1 andC/C++ pointers part 2.
  4. Also the exercises in theIndirection Operator lab worksheet 1,lab worksheet 2 and labworksheet 3.

 

 

 

 

|< C & C++ Arrays 2 | Main | C & C++ Pointers 2 >|Site Index |Download |


C and C++ Pointers: Part 1 | Part 2 | Part 3 |