
-----------------------------------
HazySmoke)345
Thu Oct 21, 2010 5:23 pm

Difference between array and pointer
-----------------------------------
Is there a difference between

[code]int *a; 
int b[10];
[/code]

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
[/code]

Because here, b does NOT need a size, and b CAN be modified. Aren't arrays essentially pointers?

-----------------------------------
DtY
Thu Oct 21, 2010 6:27 pm

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.
int *a;
a = malloc(5 * sizeof(int));
*a; /* a

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:
int a
The memory will automatically be freed when a goes out of scope.

There is, though, one way they are different:
int **a;
int a
Are the same (in a function declaration, I'm pretty sure aint a
You cannot pass a to a function that expects a int **a or int aint 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
Tue Oct 26, 2010 8:32 pm

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
Tue Feb 22, 2011 7:51 pm

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. ;)
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:
#include 
#include 

void foo1(int* a) {
    printf("sizeof(a) == %d\n", sizeof(a));
}

void foo2(int a
