1

I have 3 arrays defined as follows:

typedef struct heapMemNode
{
    int *coord;
    int x, y;
    int *prevcoord;
    int px, py;
    int d;
    char tp;
    int *telcoord;
    int c;
} hmemn;

hmemn *heapmem = calloc((siz2+1), sizeof(hmemn));
int *heaploc = calloc((siz2+1), sizeof(hmemn *));
int ***coords = (int ***)calloc((siz2+1), sizeof(int **));

for(i = 0; i<=siz2; i++){
    coords[i] = (int**)calloc((siz2+1),sizeof(int*));
}

I want to be able to access elements in the heapmem array, e.g., heap[].tp, by entering coordinates in the coords array and following the pointer from this array to the heaploc array and the pointer from the heaploc array to heapmem where tp is accessed but I can't find the syntax to do this.

1 Answer 1

1

This isn't the answer you want to hear.

As a rule of thumb: whenever you find yourself writing more than 2 levels of indirection in a C program, you know that your program design is flawed and needlessly obscure. There exist no case where you ever need three levels of indirection.

So the right thing to do here is to step back from the keyword and specify the actual problem you want to solve. It would seem that you are trying to create some some dynamic memory allocation scheme? Or maybe a heap abstract data type? I don't quite see how the code posted would fit in with either of those two heap concepts.

What is the array a model of? Is there an actual need to interpret the array as a multi-dimensional matrix? Typically you do so when you want to access the array by arr[x][y].

But if that is the case, then does it make sense to store a whole lot of coordinates inside the struct itself? Are you trying to implement some sort of index table inside the multi-dimensional array? How does that make sense?

Why do you have variables called x and y inside an array which is accessed by x and y? And so on.

(Also, casting the result of malloc doesn't make any sense either)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.