@Ajaycpp
True, they can often be used interchangeably, but not all the time.
(Actually I'm not sure your first line up there is legal, but let's stick with pointers versus arrays)
A pointer generally holds the address of one object in memory, while an array is an aggregate of the same type of objects. The address of the first object in the array also serves as the address of the entire array (just like the address of the first byte in a 4-byte word would be the address of the whole word), this lets us use the name of the array as a synonym for a pointer to its first object. The "arrayness" of the array introduces some differences however, for example a common paradigm for finding the
size of an array goes like so:
1 2
|
int arr[] = {1,2,3,4,5,6};
int arrSize = sizeof(arr)/sizeof(arr[0]);
|
If you were treating arr as a pointer, then you'd get different values, for example
sizeof(arr)
is not the same as
sizeof(&arr[0])
sizeof will return different values for both examples below:
1 2
|
int a[]= {1,2,3,4,5,6,7,8,9,10}; // sizeof(a) = 8 * sizeof(int)
int *b=(int *)malloc(10*sizeof(int)); // sizeof(b) = sizeof (int *)
|
Also, a pointer is a variable but an array name is not. So using the example above, you could do:
b = a;
or
b = &a[0];
but not a = b;
- This is also why your first line up there won't work.