Exceptions where arrays are not treated as a pointer

We understood that an array is not really a pointer, but compiler behaves if it was a pointer.
As I said there are three exceptions to this rule:

1) When using of sizeof():
It is obvious in this example:

1
2
3
4
5
6
 
int arr[3];
cout<<sizeof(arr); //arr is the array itself, not an address

/* output */
12


So you can conclude, to find out the number of elements in an array we can use this formule:

 
int numOfelements = sizeof(arr)/sizeof(arr[0]);


2) When using &(address of) operator:
When we write &array, compiler does not take the array as an address it takes it as it is, as an array and looks for it's address. (The address of the array is the address of it's first element.)
So the type of &array undoubtly will be "pointer to array".

3) When it is a right hand string literal to initialize charachter:
It is obvious in this example:

 
char charstring[]= "In C++ this like strings are array!"


If the string was taken as an address, the compiler would generate an error.
Topic archived. No new replies allowed.