5

I am new to C++ and as I was reading MIT lecture note about pointer I recognize something strange:

Pointers are just variables storing integers – but those integers happen to be memory ad­dresses, usually addresses of other variables. A pointer that stores the address of some variable x is said to point to x. We can access the value of x by dereferencing the pointer.

well and also I find that the pointer can have a type:

int *pointer ; 
char * pointer ; //example 

well it just said it's an int that hold an address why give it the same type as the thing it point at if it's just hold a reference to it not an actual value ?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Hesham Adel
  • 367
  • 4
  • 16
  • 1
    What you read is right. Pointers are just storage space to store memory addresses. You can even store address to an integer variable with some casts `char c = 'A'; int ptr = (int)&c;`. Now `ptr` holds the address to `c`. But the point here is you cannot dereference `ptr` and get the value of `c` – Karthik Kalyanasundaram Feb 23 '14 at 02:33
  • It boils down to the same reason we have `int` and `char` instead of just `var` or something. – chris Feb 23 '14 at 02:34
  • @KarthikKalyanasundaram try that on a platform where `sizeof(int)` and `sizeof(char*)` aren't the same. The claim in the citation of "just storing integers" is assumptive at-best, misleading, and in many cases outright wrong. Frankly I'm disappointed in MIT if that is truly their citation. – WhozCraig Feb 23 '14 at 02:36
  • @WhozCraig Yes, I know that. I have ported a project from Win32 to x64 and in the source code, lot of places they have used `DWORD`s to store memory addresses. And I know the pain to fix all those. To help him understand better I made it simple – Karthik Kalyanasundaram Feb 23 '14 at 02:39
  • @WhozCraig I agree the claim is a bit surprising considering the source. I have some good quotes and a link in [my answer to the dup](http://stackoverflow.com/a/20407956/1708801) that clearly show that assuming the representation of the pointer is not a good idea. – Shafik Yaghmour Feb 23 '14 at 02:47
  • +1 this is a good question and reading many of the answers in the dup will be quite instructive. – Shafik Yaghmour Feb 23 '14 at 02:48

2 Answers2

1

Maybe you have an array of int. and "accidently" ... an array variable is a pointer to the first component of that array. With the knowledge of having a pointer being of type int, the compiler knows how far it must jump to the next data.

int a[4] = { 0, 1, 2, 3 };

// Dereferenced pointers to an int with additonal offset of size int n times
printf("%i\n", *a);
printf("%i\n", *(a+1));
printf("%i\n", *(a+2));
printf("%i\n", *(a+3));

// Equivalent to using the array as usual
printf("%i\n", a[0]);
printf("%i\n", a[1]);
printf("%i\n", a[2]);
printf("%i\n", a[3]);

This is an advanced example, but it is one of the seldom uses for an int pointer. Most often you will have pointers to objects, structs or arrays - seldom to the value types. But in some implementations of algorithms, pointers to value types can be usefull for sorting, searching etc. in arrays

Marcel Blanck
  • 867
  • 7
  • 12
  • oh wow i start to like C++ so i can use array variable as a pointer .... and it give the compiler a hint – Hesham Adel Feb 23 '14 at 04:11
  • Yes and like Proxy mentioned, you can do a lot more stuff with the pointers, even reinterpret a whole set of data, like reinterpeting a received char array directly as organized data in a struct, without copying. But don't bother now at the beginning - these are usefull tricks sometimes but not essential. Typecasting will be another topic in your future learning. – Marcel Blanck Feb 23 '14 at 19:33
0

Because the size of a pointer is dependent on the structure of the program you're writing. In a 32-bit environment, the size of a pointer is 32 bits and you can cram the memory location into an int using reinterpret_cast. But if your program is 64-bit, the size of a pointer is 64 bits and will not fit into a 32-bit integer. But you can still reinterpret_cast the pointer into a 64-bit integer, right? Well... yes -- but I'm not done yet.

Build profile aside, it's ultimately for the same reason that you don't store every pointer as a void*. It can point to an object of any type, making it useful if you're not entirely sure what kind of variable it is, but it would be nearly impossible to tell what the variable actually stores.

Proxy
  • 1,824
  • 1
  • 16
  • 27