Arrays and pointers

1
2
3
4
5
6
int* a1 = malloc(4 * sizeof(int));
int* a2 = malloc(3 * sizeof(int));
int* a3 = malloc(5 * sizeof(int));
a1[0] = 1; a1[1] = 2; a1[2] = 3; a1[3] = 4;
a2[0] = 9; a2[1] = 8; a2[3] = 7;
a3[0] = -1; a3[1] = -2; a3[2] = -3; a3[3] = -4; a3[4] = -5;


My question:

Are a1, a2 and a3 arrays? I think they are pointers that point to an array right?

Secondly, If a1, a2, and a3 are considered to be arrays, are they of the same size?
Yep. They are pointers to arrays.

Arrays are only considered the same size depending on what you mean by "size". They may occupy the same number of bytes in memory, or they may have the same number of elements. (Since the above is neither, then they are all disparate sizes.)

Hope this helps.
No, I disagree with Duoas; a1, a2 and a3 aren't arrays but I agree that they are not the same size.

all you have is three lumps of memory that are the size of 4, 3, and 5 ints.

You could use them as arrays or store any data type or structure in these memory areas.
Last edited on
I didn't say that a1, a2, and a3 are arrays. I said that they point to arrays.

An array is only an array when treated as if it is an array. C and C++ give us the option of choosing how many items a pointer references.

;-)
OK, let you off :)
Whew. I was worried for a moment there that my brain wasn't working right. I hate it when it does that. (Or is it "when it doesn't that"?)

:-P
Topic archived. No new replies allowed.