0

I already have an idea on how to malloc a matrix if it were an int**. But using a typedef, I think have an idea but I'm not so sure.

typedef int LabeledAdjMatrix[SIZE][SIZE]; 

Do I do it like this?

APSP = (APSPMatrix*)malloc(sizeof(APSPMatrix));

But when I access it I'm gonna have to use *APSP[0][0] and I have no idea how to use this in memset/memcpy.

Is there a proper way of doing this? Both in dynamically allocating and in accessing.

1 Answers1

1

My advice would be to not use array typedefs, they make the code harder to read as it is less apparent when array-pointer decay is or isn't happening.

If you want to allocate a contiguous array you can write:

int (*APSP)[SIZE] = malloc( sizeof(int[SIZE][SIZE]) );

and then access it as APSP[0][0].

Your post talks about "malloc as if it were int **", by which I assume you mean you want separate allocations for each rows... but then you would write int **APSP and write a loop to allocate each row, it is really nothing to do with [SIZE][SIZE].

M.M
  • 138,810
  • 21
  • 208
  • 365