Created
December 19, 2012 13:02
-
-
Save johntyree/4336530 to your computer and use it in GitHub Desktop.
3D array indexing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <assert.h> | |
int main(int argc, char* argv[]) { | |
int idim = 3; | |
int jdim = 4; | |
int kdim = 5; | |
int footprint = idim*jdim*kdim; | |
int *arr = malloc( footprint * sizeof(int)); | |
int *mini; | |
for (int i = 0; i < footprint; i++) { | |
arr[i] = i; | |
} | |
for (int i = 0; i < idim; i++) { | |
for (int j = 0; j < jdim; j++) { | |
for (int k = 0; k < kdim; k++) { | |
int idx = i*(jdim*kdim) + j * (kdim) + k; | |
assert(idx < footprint); | |
printf("%2i ", arr[idx]); | |
} | |
printf("\n"); | |
} | |
printf("\n"); | |
} | |
int slice = 2; | |
int row = 2; | |
mini = arr + (slice*(jdim*kdim)) + row*(kdim); | |
printf("\n"); | |
printf("Slice %i, Row %i\n", slice, row); | |
for (int i = 0; i < kdim; ++i) { | |
printf("%i ", mini[i]); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment