Arrays and pointers

Jul 12, 2008 at 4:38pm
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?
Jul 12, 2008 at 9:11pm
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.
Jul 12, 2008 at 9:15pm
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 Jul 12, 2008 at 9:31pm
Jul 12, 2008 at 9:56pm
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.

;-)
Jul 12, 2008 at 10:01pm
OK, let you off :)
Jul 13, 2008 at 12:44am
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.