Originally Posted by
quzah
In the first, you declare a pointer to an array of the specified side array, in the second, you are actually allocating the array.
Quzah.
Correct, hence in my previous post I referred to the array declaration as dynamic, meaning I allocated memory for the array at runtime.
Whereas, the second array is declared staticly, meaning the memory allocation for the array occurs when the program is compiled/linked into an executable file, making it a much larger executable file than when I dynamically allocated the memory for the array.
I got the idea for dynamically declaring a 2 dimensional array from an example program in the book "Using Borland C++ 3", 2nd Edition, by Lee Atkinson and Mark Atkinson, Que Corporation, 1992 (Library of Congress Catalog No.: 91-67634, ISBN: 0-88022-901-2)
Node101
Here is the example program
(Listing 4.7. newdyna.c. Using array subscripts with a dynamically allocated array):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
#define MAXX 8
#define MAXY 8
main()
{
int (*iarray)[MAXX][MAXY];
size_t nmemb;
int i, j, k = 0;
clrscr();
nmemb = MAXX * MAXY;
if ( NULL == ( iarray = calloc( nmemb, sizeof( int ) ) ) ) {
printf( "Not able to allocate memory.\n" );
printf( "Aborting program." );
abort();
}
for ( i = 0; i < MAXX; i++ ) {
for ( j = 0; j < MAXY; j++ ) {
(*iarray)[i][j] = k++;
}
}
for (i = 0; i < MAXX; i++ ) {
for ( j = 0; j < MAXY; j++ ) {
printf( "%4d ", (*iarray)[i][j] );
}
printf( "\n" );
}
free( iarray );
return 0;
}