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.

5
  • 1
    what is APSPMatrix ? Did you mean LabeledAdjMatrix ? Commented Oct 10, 2016 at 5:52
  • Oh shoot yes. There was a #define APSPMatrix LabeledAdjMatrix. I forgot to include that woops. Commented Oct 10, 2016 at 6:02
  • *APSP[0][0] --> (*APSP)[0][0] Commented Oct 10, 2016 at 6:29
  • @BoredChinese: I wrote a specific answer for your question in the duplicate... Hope it helps Commented Oct 10, 2016 at 6:50
  • @SergeBallesta thank you for the answer! I can't reply directly to that post of yours since my reputation is below 50 haha. Commented Oct 10, 2016 at 8:14

1 Answer 1

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].

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

1 Comment

Unfortunately, it's my professor who made the typedef and not me and I'm supposed to use it. :-(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.