Im trying to use malloc to allocate memory for a multi dimensional array of ints. After this code has finished i want to be able to address it with plane[x][y]. Does any one know what i'm doing wrong here?
Code:
// Declare variables
int plane_width = 10, plane_height = 10, x, y;
int * plane;
// Allocate memory for first dimension of plane
if ( ( ( plane = malloc( sizeof( int * ) * plane_width ) ) == NULL ) ) {
fprintf( stderr, "No memory available\n" );
exit( EXIT_FAILURE );
}
// Allocate memory for second dimension of plane
for ( x = 0; x < plane_width; x++ ) {
if ( ( ( plane[x] = ( int * ) malloc( sizeof( int ) * plane_height ) ) == NULL ) ) {
fprintf( stderr, "No memory available\n" );
exit( EXIT_FAILURE );
}
// Set tile to default value
for ( y = 0; y < plane_height; y++ ) {
plane[x][y] = TILE_UNINITIALISED;
}
}