UMBC CS 201, Fall 06
UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

Two-dimensional Arrays

A two-dimensional array of ints is really a one dimensional array of pointers to int. Each of the elements is a pointer to the memory location of the beginning of a one-dimensional array of ints.
Pictorially, we have

We can declare a two-dimensional array of ints easily :

int array[ROWS][COLS]; but if we need to dynamically allocate a two dimensional array of ints, we have to do that in a couple of steps.
  1. Allocate the space needed to hold the array of pointers, one int* for each row of the array.
  2. Allocate enough space for one of the rows, one int for each column, & store the address of this memory in the array of pointers
  3. Repeat step #2 once more for each remaining row
Since you also must free() all memory you've allocated, when you've finished using the array, you'll need to free all of the memory pointed to by the pointers in the array and then free the array itself. /********************************* ** File 2darray.c ** Author: S. Evans ** Date: 10/31/06 ** Section: 02XX & 201H 0101 ** E-Mail: bogar@cs.umbc.edu ** ** This file demonstrates dynamic memory ** allocation of a 2 dimensional array ** of integers *******************************/ #include <stdio.h> #include <stdlib.h> int **GetTwoDSpace(int rows, int cols); void FreeTwoDSpace(int **array, int rows); int main() { int **array, i, j, rows, cols; printf("This program dynamically allocates the space needed\n"); printf("for a two-dimensional array of ints\n\n"); /* How big would you like the array to be ? */ printf("How many rows : "); scanf("%d", &rows); printf("How many columns : "); scanf("%d", &cols); printf("\n"); /* Get the space for the array */ array = GetTwoDSpace(rows, cols); /* Load the array with values */ for(i = 0; i < rows; i++) { for(j = 0; j < cols; j++) { array[i][j] = i * j; } } /* Print out what the array holds */ for(i = 0; i < rows; i++) { for(j = 0; j < cols; j++) { printf("%5d", array[i][j]); } printf("\n"); } /* Free the space that was dynamically allocated */ FreeTwoDSpace(array, rows); return 0; } /*************** ** Function: GetTwoDSpace() ** ** GetTwoDSpace() gets the memory space needed to hold the 2 dimensional ** array of ints and returns the pointer which will become the name of ** the array. ** ** Inputs: Number of rows to be in the array ** Number of columns to be in the array ** Output: The pointer to the memory ********************************/ int **GetTwoDSpace(int rows, int cols) { int **array, i; /* Get the space for the array of pointers to int */ array = (int**)malloc(rows * sizeof(int*)); if(array == NULL) { fprintf(stderr, "Error getting space for 2-d array of ints\n"); exit(-1); } /* Get the space for all of the one dimensional arrays of ints */ for(i = 0; i < rows; i++) { array[i] = (int*)malloc(cols * sizeof(int)); if(array[i] == NULL) { fprintf(stderr, "Error getting space for 2-d array of ints\n"); exit(-1); } } return array; } /*************** ** Function: FreeTwoDSpace() ** ** FreeTwoDSpace() frees the memory space of a 2 dimensional ** array of ints. ** ** Inputs: Number array to be freed ** Number of rows in the array ** Output: None ********************************/ void FreeTwoDSpace(int **array, int rows) { int i; for(i = 0; i < rows; i++) { free(array[i]); } free(array); }

output

This program dynamically allocates the space needed for a two-dimensional array of ints How many rows : 3 How many columns : 5 0 0 0 0 0 0 1 2 3 4 0 2 4 6 8


CSEE | 201 | 201 F'06 | lectures | news | help

Tuesday, 31-Oct-2006 21:13:22 EST