Confusion between “int array[int]” vs“ int *array”

  • A+
int array[100];  int *array; 

So I am a litle confused with the diferences between int array[100] vs int * array.

Essentialy when I do int array[100] (100 it's just an example of an int), I just reserved space in memory for 100 ints, but I can do int * array and I didn't specify any type of size for this array, but I can still do array[9999] = 30 and that will still make sense.

So what's the diference between these two?


A pointer is a pointer, it points somewhere else (like the first element of an array). The compiler don't have any information about where it might point or the size of the data it might point to.

An array is, well, an array of a number of consecutive elements of the same type. The compiler knows its size, size it's always specified (although sometimes the size is only implicitly specified).

An array can be initialized, but not assigned to. Arrays also often decays to pointers to their first element.

Array decay example:

int array[10];  int *pointer = array;  // Here the symbol array decays to the expression &array[0]  // Now the variable pointer is pointing to the first element of array 

Arrays can't naturally be passed to function. When you declare a function argument like int arr[], the compiler will be translating it as int *arr.

All of this information, and more, should be in any good book, tutorial or class.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: