So this is one of the first times I've worked with multi-dimensional arrays in C. I have a 3D array of ints that is being filled and I want to check to make sure the values are turning out alright, but I'm having some issues after the array is filled. Some similar code of what's going on to explain the situation:
int a = 10;
int b = 20;
int c = 30;
int* valuesPtr;
valuesPtr = (int*) malloc(a * b * c * sizeof(int));
functionThatFillsValues(valuesPtr); // this function expects a *int, and works.
Everything here is working properly. Then, I'm printing out the value like so:
printf("0,0,0: %d\n", *valuesPtr);
This successfully prints the value at [0][0][0]. Doing the following works:
printf("0,0,1?: %d\n", *(valuesPtr+1));
Now, I'm to understand from https://www.geeksforgeeks.org/pointer-array-array-pointer/ there's a few ways to get values from where this pointer is pointing to.
The following works: printf("0,0,0: %d\n", valuesPtr[0]);
but as soon as I try to go any further than that, it breaks on make. None of the following work:
printf("0,0,0: %d\n", valuesPtr[0][0];
printf("0,0,0: %d\n", valuesPtr[0][0][0];
printf("0,1,0: %d\n", valuesPtr[0][1];
printf("0,0,1: %d\n", valuesPtr[0][0][1];
printf("%d\n", *(*(valuesPtr+1)+1));
printf("%d\n", *(*(*(valuesPtr+1)+1)+1));
With the [] ones getting the error "subscripted value is neither array nor pointer", and the *() attempts getting the error "invalid type argument of ‘unary *’ (have ‘int’)" I'm guessing because in both cases, it's resolving both valuesPtr[0] and *(valuesPtr) as an int, and then not going any further. I'm guessing the issue is that valuesPtr is declared as a *int, so it believes it's just a pointer to an int. How can I get this to understand it's a pointer to a 3D array, either by getting the pointers to the 2D or 1D arrays inside, or just getting it to understand I can use [][][] notation?
Would something like this be looking at the right spot?
printf("i,j,k: %d", *(valuesPtr+(i*(c*b))+(j*(c))+k)