#include <stdio.h>
#include <stdlib.h>
#define ERROR -1
#define OK 0
int InitOneDimArray(void** arr, int len, int itemSize)
{
*arr = (void *)malloc(len * itemSize);
if (*arr == NULL) {
return ERROR;
}
return OK;
}
void DestroyOneDimArray(void** arr)
{
if (*arr != NULL) {
free(*arr);
*arr = NULL;
}
return;
}
int InitTwoDimArray(void*** arr, int rowNum, int colNum, int itemSize)
{
*arr = (void **) malloc(rowNum * sizeof(void *));
if (*arr == NULL) {
return ERROR;
}
for (int index = 0; index < rowNum; index++) {
(*arr)[index] = (void *)malloc(colNum * itemSize);
if ((*arr)[index] == NULL) {
return ERROR;
}
}
return OK;
}
void DestroyTwoDimArray(void*** arr, int rowNum)
{
if (*arr == NULL) {
return;
}
for (int index; index < rowNum; index++) {
if ((*arr)[index] != NULL) {
free((*arr)[index]);
}
}
free(*arr);
*arr = NULL;
return;
}
#define LENGTH 10
int main(int args, char* argv[])
{
int* oneDimArr;
int** twoDimArr;
InitOneDimArray((void **)&oneDimArr, LENGTH, sizeof(int));
for (int index = 0; index < LENGTH; index++) {
oneDimArr[index] = index;
}
printf("%d \n", oneDimArr[5]);
DestroyOneDimArray((void **)&oneDimArr);
InitTwoDimArray((void ***)&twoDimArr, LENGTH, LENGTH, sizeof(int));
for (int i = 0; i < LENGTH; i++) {
for (int j = 0; j < LENGTH; j++) {
twoDimArr[i][j] = i;
}
}
printf("%d \n", twoDimArr[5][5]);
DestroyTwoDimArray((void ***)&twoDimArr, LENGTH);
return 0;
}