Skip to content

Instantly share code, notes, and snippets.

@mu8086
Created June 5, 2022 00:05
Show Gist options
  • Save mu8086/ec8127a8ed79126772356adc2d48d1d5 to your computer and use it in GitHub Desktop.
Save mu8086/ec8127a8ed79126772356adc2d48d1d5 to your computer and use it in GitHub Desktop.
[C] 3dArray
char ***char3dArray(int length, int width, int height) {
// ret[height][width][length]
char ***ret = (char ***) malloc(sizeof(char **) * height + sizeof(char *) * height * width + sizeof(char) * height * width * length);
char **first_row = (char **)(ret + height);
char *first = (char *)(first_row + height * width);
memset(first, 0, sizeof(char) * height * width * length);
for (int i=0, j, wl = width * length; i<height; ++i) {
ret[i] = first_row + i * width;
for (j=0; j<width; ++j) {
ret[i][j] = first + i * wl + j * length;
}
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment