Click here to Skip to main content
12,755,080 members (39,587 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 27 Nov 2005

Allocating memory to a 'Pointer to Pointer' variable

, 27 Nov 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
Describes how to allocate memory to and retrieve values from pointer to pointer variables.


I am hoping this article would help beginners. It describes how memory can be dynamically allocated, how values are assigned or retrieved, and how memory is freed up for 'pointer to pointer' variables.

Pointers, Reference operator (&), and Dereference operator (*)

Before discussing about 'pointer to pointer' variables, we will see what a pointer variable is and what the reference operator (&) and dereference operator (*) are used for.

A pointer variable can be declared as:

type* variable;


int* a;

The above declaration means that the pointer variable "a" can hold the address of memory allocated to an integer variable.

int val;
printf("%d", *val);
printf("%d", *val);

In the above sample code, the reference operator (&) is used to refer the address of the memory location assigned for the integer variable 'val'. Also, the dereference operator (*) is used to point to the content of the memory location whose address is held by the pointer variable 'val'.

The first printf statement prints '10', and the second printf statement prints '15'.

Pointer to pointer variables

A pointer to pointer variable can hold the address of another pointer variable. It can be declared as:

type** variable;

Allocating memory

Think of the memory to be allocated to a pointer to pointer variable as two dimensional. It has 'rows' and 'columns'; i.e., if the size is m x n, there will be 'm' rows, and for each row, there will be 'n' columns.

  1. First, you have to allocate memory for 'm' rows.
  2. Secondly, you have to allocate 'n' columns for each of the 'm' rows.

We will take an example for allocating memory to a pointer to pointer to float values. Let the number of rows be '4' and the number of columns '3'.

float **float_values;
//allocate memory for rows
float_values = (float**)malloc(4 *sizeof(float*));
//for each row allocate memory for columns
for(int i=0; i<4; i++)
   *(float_values+i) = (float*)malloc(3 *sizeof(float));

2 dimensional view

Assigning values

We need to know how we can access each location before assigning values to them.

There are two ways to access these two dimensional memory locations.

  1. Use the [] operator in the same way we use for two dimensional arrays.
  2. float val;
    for(int i=0;i<4;i++)
        for(int j=0;j<3;j++)
            scanf("%f", &val);
            float_values[i][j] = val;
  3. Use the pointer operator ("*")
  4. float val;
    for(int i=0;i<4;i++)
       for(int j=0;j<3;j++)
           scanf("%f", &val);
           *(*(float_values+i)+j) = val;

Display or retrieve values

For retrieving, we can use any of the two operators which we used for assigning values. An example for using the "*" operator is given below.

for(int i=0; i<2; i++)
     for(int j=0; j<3; j++)
         printf("%f\n", *(*(floats+i)+j));

Freeing up the allocated memory

We should not forget to free the memory which we have allocated dynamically, starting from the lowest level, i.e., in the reverse order of allocations. If we free up the higher level pointers first, the lower level pointers would be lost and we would not be able to free up the memory allocated to those pointers. This would lead to memory leaks. Given below is how we should free up memory:

//freeing up memory allocated to second level pointers
for(int i = 0; i < 4; i++)
//freeing up memory allocated to first level pointers


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rajesh R Nair
Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
beginnergSOAP12-Jan-12 2:53
memberbeginnergSOAP12-Jan-12 2:53 
GeneralMy vote of 1 Pin
phuong303010-Jan-12 22:58
memberphuong303010-Jan-12 22:58 
GeneralMy vote of 5 Pin
Athrej17-Apr-11 8:44
memberAthrej17-Apr-11 8:44 
General*val should be *a Pin
clausscheiblauer9-Oct-10 0:52
memberclausscheiblauer9-Oct-10 0:52 
GeneralAllocating memory Pin
MidNightCoder01019-Aug-10 9:33
memberMidNightCoder01019-Aug-10 9:33 
Generalxor cipher code Pin
Sadru30-Aug-09 15:53
memberSadru30-Aug-09 15:53 
Generalhelp for beginner Pin
sihotamarpal28-Aug-06 3:34
membersihotamarpal28-Aug-06 3:34 
Generalwhy you did not get higher rating ... Pin
Maximilien28-Nov-05 3:43
memberMaximilien28-Nov-05 3:43 
GeneralRe: why you did not get higher rating ... Pin
Rajesh R Nair28-Nov-05 5:24
memberRajesh R Nair28-Nov-05 5:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 27 Nov 2005
Article Copyright 2005 by Rajesh R Nair
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid