cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Articles : Exceptions where arrays are not treated ...
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programm...
Articles
Lounge
Jobs

-

post  Exceptions where arrays are not treated as a pointer

siavoshkc (6)
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.
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us