Original Post
🔒 Locked
Multi-Dimensional Arrays
Advertisement
Have a look at these threads:
Multi Dimension arrays in C and/or C++
topic_id=96031
topic_id=95830
topic_id=88357
topic_id=85789
topic_id=81733
topic_id=67659
topic_id=67283
topic_id=53686
topic_id=53486
topic_id=53066
You can search the forums, btw
Multi Dimension arrays in C and/or C++
topic_id=96031
topic_id=95830
topic_id=88357
topic_id=85789
topic_id=81733
topic_id=67659
topic_id=67283
topic_id=53686
topic_id=53486
topic_id=53066
You can search the forums, btw
Here''s how you do it in C, YMMV
// 2D array allocation
#include <stdlib.h>
#include <stdio.h>
/*-------------------------------------------------------------------------------------
FUNCTION : array_int_2D(int nrows, int ncols)
PURPOSE : Template for allocating arrays. Could be used for any 2D array.
RETURN : Start address of array
PARAMS : nrows - number of rows
: ncols - number of columns
--------------------------------------------------------------------------------------*/
int **array_int_2D(int nrows, int ncols)
{
int i, j;
// allocate the memory for rows
int **arr = malloc(nrows * sizeof(int *));
if(!arr) return 0;
printf("Pointers to Rows allocated: %p\n", arr);
// allocate the rows i.e. number of columns/row
for(i = 0; i < nrows; i++){
arr[i] = malloc(ncols * sizeof(int));
if (!arr[i]){
for (int k=0; k<(i-1); k++)
free(arr[k]); // release memory for all previously allocated
free(arr);
return 0;
}
printf("Row %2d allocated: %p\n", i, arr[i]);
}
// fill in some values
for (i=0; i < nrows; i++)
for (j=0; j < ncols; j++)
arr[i][j] = (i+1) * (j+1); // rows
return arr;
}
/*-------------------------------------------------------------------------------------
FUNCTION : array_free_2D(int ***arr, int nrows, int ncols)
PURPOSE : Free memory allocated for 2D array.
RETURN : NONE
PARAMS : arr - pointer to 2D array
: nrows - number of rows
: ncols - number of columns
--------------------------------------------------------------------------------------*/
void array_free_2D(int ***arr, int nrows, int ncols)
{
int **array = *arr; // dereference pointer to array
if (array){
for( int i = 0; i < nrows; i++){
free(array[i]);
printf("Row %2d de-allocated: %p\n", i, array[i]);
}
free(array);
printf("Pointers to Rows de-allocated: %p\n", array);
*arr = 0;
}
}
int main (void)
{
int i, j;
int nrows = 24;
int ncols = 8;
int **matric;
matric = array_int_2D(nrows, ncols);
printf("\n------------------------------------------------------------------------------\n");
printf("Dumping array contents\n");
printf("\n------------------------------------------------------------------------------\n");
if (matric){
printf("Column |");
for (i=0; i < ncols; i++)
printf("\t%3d",i);
printf("\n------------------------------------------------------------------------------\n");
for (i=0; i < nrows; i++) {
printf("Row %2d |", i);
for (j=0; j < ncols; j++)
printf("\t%3d", matric[i][j]);
printf("\n");
}
}else{
printf("No mem");
}
printf("\n------------------------------------------------------------------------------\n");
// release the memory
printf("Address of array before free %p\n", matric);
array_free_2D(&matric, nrows, ncols);
printf("Address of array after free %p\n", matric);
return 0;
}
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement