Difference between array and pointer
Author |
Message |
HazySmoke)345
|
Posted: Thu Oct 21, 2010 5:23 pm Post subject: Difference between array and pointer |
|
|
Is there a difference between
other than the fact that b needs a size, and b can't be modified?
I'm asking because I'm wondering if these two are EXACTLY the same thing.
code: | int **a; //pointer to a pointer
int (*b)[]; //pointer to an array
|
Because here, b does NOT need a size, and b CAN be modified. Aren't arrays essentially pointers? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DtY
|
Posted: Thu Oct 21, 2010 6:27 pm Post subject: RE:Difference between array and pointer |
|
|
They're *pretty* much the same thing. Arrays are implemented using pointers.
In fact, an early version of C (before it was standardized?) didn't even have arrays, you had to use the pointer syntax.
C: | int *a;
a = malloc(5 * sizeof(int));
*a; /* a[0] */
*(a+1) /* a[1] */
*(a+n) /* a[n] */
[...]
free(n); |
Array syntax is just syntactic sugar. The only real advantage (that is, something you just couldn't do before) of the array syntax is that when you create the array:
The memory will automatically be freed when a goes out of scope.
There is, though, one way they are different:
Are the same (in a function declaration, I'm pretty sure a[][] will not work in the body), but, if you create:
You cannot pass a to a function that expects a int **a or int a[][] because of how multidimensional arrays are created--they're not pointers to pointers, they're a single continuous block of memory, if you want to pass it to a function, the function must be expecting an array with the same major size, either int a[][5] or int *a[5].
BUT, you can also create multidimensional arrays that *are* pointers to pointers, the advantage to this is that each member of the array can have a differently sized array, this is commonly used to create arrays of strings, like the argument you get to main:
C: | int main(int argc, char **argv);
int main(int argc, char argv[][]);
int main(int argc, char *argv[]); |
are all acceptable, because what you are getting is a pointer pointer, and not a *proper* two dimensional C array. |
|
|
|
|
|
HazySmoke)345
|
Posted: Tue Oct 26, 2010 8:32 pm Post subject: Re: Difference between array and pointer |
|
|
Ah, that explains. So the difference arises when we deal with higher-dimensional arrays.
Thank you for answering my question =) You get a karma point for that. |
|
|
|
|
|
Xupicor
|
Posted: Tue Feb 22, 2011 7:51 pm Post subject: Re: Difference between array and pointer |
|
|
A pointer and an array are not the same thing, although an array is usually implicitly converted to a pointer.
ISO/IEC 9899:TC3 6.3.2.1 Lvalues, arrays, and function designators wrote: Except when it is the operand of the sizeof operator or the unary & operator, or is a
string literal used to initialize an array, an expression that has type ??array of type?? is
converted to an expression with type ??pointer to type?? that points to the initial element of
the array object and is not an lvalue.
Thus you can see why an array is always passed by reference - not the C++ one of course. In detail: it is converted to a pointer, and the pointer is passed by value, the usual way. It doesn't matter which notation in function you'll use.
See:
c: | #include <stdio.h>
#include <stdlib.h>
void foo1 (int* a ) {
printf("sizeof(a) == %d\n", sizeof(a ));
}
void foo2 (int a [10]) {
printf("sizeof(a) == %d\n", sizeof(a ));
}
void foo3 (int a []) {
printf("sizeof(a) == %d\n", sizeof(a ));
}
int main (void) {
int* a = calloc (10, sizeof(int));
int b [10];
printf("sizeof(a) == %d\n", sizeof(a ));
printf("sizeof(b) == %d\n", sizeof(b ));
foo1 (a );
foo1 (b );
foo2 (a );
foo2 (b );
foo3 (a );
foo3 (b );
free (a );
return 0;
} |
|
|
|
|
|
|
|
|