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?
JaWiB
Jul12-08, 05:28 PM
>Are a1, a2 and a3 arrays? I think they are pointers that point to an array right?
Well, pointers that point to the first element in an array. You can do pointer arithmetic to accomplish the same thing the subscript operator does; e.g.:
*(a1+1) = 2;
>Secondly, If a1, a2, and a3 are considered to be arrays, are they of the same size?
If you consider them as arrays, then I would say no; you obviously shouldn't access a1[4] but you can access a3[4]. On the other hand, if you use sizeof on them, you'll get the same thing (sizeof(int*)).
ladesidude
Jul12-08, 05:43 PM
>Are a1, a2 and a3 arrays? I think they are pointers that point to an array right?
Well, pointers that point to the first element in an array. You can do pointer arithmetic to accomplish the same thing the subscript operator does; e.g.:
*(a1+1) = 2;
>Secondly, If a1, a2, and a3 are considered to be arrays, are they of the same size?
If you consider them as arrays, then I would say no; you obviously shouldn't access a1[4] but you can access a3[4]. On the other hand, if you use sizeof on them, you'll get the same thing (sizeof(int*)).
i did use sizeof() and they all came back as 4, since the size of an int is 4 bytes and since its pointing to the first element in the array, I think. So its safe to say that they are not of the same size?
Hurkyl
Jul12-08, 05:46 PM
i did use sizeof() and they all came back as 4, since the size of an int is 4 bytes and since its pointing to the first element in the array, I think.
Try it with other types of pointers....
DaleSpam
Jul12-08, 08:27 PM
The name of an array is a constant pointer to the first element of the array. In your example a1, a2, and a3 are not constant, so they are not exactly the same as the name of an array. If you had declared them constant then, for all practical purposes, they would be the names of arrays.
Hurkyl
Jul12-08, 08:44 PM
If you had declared them constant then, for all practical purposes, they would be the names of arrays.
Except, of course, for the eminently practical fact that they will need to be freed. That they won't be allocated on the stack could be relevant. And, of course, the fact that sizeof a1 will return the size in bytes of a pointer rather than the array that was allocated.
DaleSpam
Jul12-08, 08:54 PM
Except, of course, for the eminently practical fact that they will need to be freed. Yeah, there is that :redface:
irongreat
Jul12-08, 10:06 PM
I tell you my answer. a1,a2,a3 are all arrays. But they are allocated memory in heap not stack.
Jeff Reid
Jul12-08, 11:12 PM
They are pointers, not arrays.
Here's an example of the difference:
int *p1, *p2;
int a1[5], a2[5];
p1 = p2; // copies a pointer
a1 = a2; // copies an entire array
Hurkyl
Jul12-08, 11:30 PM
a1 = a2; // copies an entire array
Actually, that one is just a syntax error.
Jeff Reid
Jul15-08, 04:30 PM
Actually, that one is just a syntax error.That used to work with older C (not C++) compilers. An alternate example using structure instead of array (this works with Visual Studio Express):
typedef struct {
int i[5];
}SI5, *PSI5;
static SI5 si0;
static PSI5 psi0;
int main(int argc, char **argv)
{
SI5 si1;
PSI5 psi1;
si1 = si0; // copy a structure
psi1 = psi0; // copy a pointer
return(0);
}
Getting back to the array example, it can be treated similar to pointer, when passed to a function. As Hurkly pointed out, an array name can't be used for assignment. The sizeof an array is different than the size of a pointer to an array.
#include <stdio.h>
void x(int *, int *);
int main(int argc, char **argv)
{
int ai0[5];
int *pai0 = ai0;
printf ("%d %d\n", sizeof(ai0), sizeof(pai0));
x(ai0, pai0);
return(0);
}