0

G'day!

So if I declared the array like this:

int *A[5];

and did this:

A[0]=(int*)malloc(sizeof(int)*10);

Can I access/populate via A[0][5] with the square bracket notation?

2 Answers 2

1

Yes, you can access A[0][n] as long as that [n] does not run past the allocated memory region.

Maybe it's helpful to remember that in c, the index will be 1 less than the number of memory locations allocated.

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

Comments

0

For the most parts, arrays and pointers are interchangeable (as arrays decays to pointers), and that includes the syntax to access them. So yes, you can use the array-indexing syntax to access a pointer.

In fact, doing a[x] is equivalent to doing *(a + x).

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.