Pointers

Can someone tell me how the size of a pointer changes when it is pointing at, say, a single long integer and when it is pointing at a dynamically declared array of long integers?

Thanks
Last edited on
closed account (1vRz3TCk)
All pointers are the same size (on any given system).

A 32-bit OS uses 32-bit pointers.
A 64-bit OS uses 64-bit pointers.
etc.
Last edited on
sizeof(my_pointer) never changes. sizeof(*my_pointer) is the size of the object that is being pointed to.
The size of the pointer doesn't change. It is always 4 bytes (32 bits). When we are looking at an array, the pointer only gives the address of the start of the array. You usually need another variable to tell you the size of the array.

Moschops wrote a good article about it here:
http://cplusplus.com/articles/EN3hAqkS/

here is a code exerpt:
1
2
3
4
5
6
7
double x[10]; // an array of 10 doubles
double* p = x; // p is a pointer, now pointing at the first double
for (int i=0;i<10;i++)
{
  std::cout << *p << std::endl;  // output the double value
  p++; // make the pointer point to the next one
}
Last edited on
Thanks guys. That's really helpful.
Topic archived. No new replies allowed.