Jakiś wyjątek z jednego z projektów...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
static void alloc_2dim_table(int ***table, size_t height, size_t width) {
size_t row;
*table = malloc(height * sizeof(int *));
assert(*table != NULL);
for(row = 0; row < height; ++row) {
(*table)[row] = malloc(width * sizeof(int));
}
}
static void dealloc_2dim_table(int ***table, size_t height) {
size_t row;
for(row = 0; row < height; ++row) {
free((*table)[row]);
}
free(*table);
}
static void fill_zero_2dim_table(int ***table, size_t height, size_t width) {
size_t row;
for(row = 0; row < height; ++row) {
memset((*table)[row], 0, width * sizeof(int));
}
}
static void show_2dim_table(int ***table, size_t height, size_t width) {
size_t row;
size_t col;
for(row = 0; row < height; ++row) {
for(col = 0; col < width; ++col) {
printf("%6d ", (*table)[row][col]);
}
(void) putchar('\n');
}
}
int main(void) {
int **table;
const size_t width = 10;
const size_t height = 10;
alloc_2dim_table(&table, height, width);
fill_zero_2dim_table(&table, height, width);
show_2dim_table(&table, height, width);
dealloc_2dim_table(&table, height);
return EXIT_SUCCESS;