pointers

Dec 23, 2013 at 9:59pm
1. How many contiguous "blocks" of addresses does compiler initialize to a pointer ? Like when we initialize an array we tell how many blocks of addresses we want to use. What about pointers?
2.
Why this code is invalid:

1
2
3
  int *s;
  s = &s; 


Pointers have addresses, don't they..? Is it because the pointer itself doesn't point to anything yet?
Dec 23, 2013 at 10:07pm
I'm not sure if I understand your first question correctly, but each pointer has only one place("address") in memory.

2. Pointers do have addresses. How otherwise would you refer to them? :)

Also, there are pointers to pointers

 
char** PointerToPointer


Every data in memory has its own adress.
Dec 23, 2013 at 10:20pm
but if we declare a pointer to an integer, then lets say what will happen with *(p+1)? does it exist? can we assign *(p+1) some value? and what about *(p+500)?
Dec 23, 2013 at 10:28pm
It's integer's arithmetics.

Each data type hold some space(e.g. char holds 1 byte, if I'm correct). But that's the only thing included in standard - you can only guess if int holds 4, 8, 16, 32, or more bytes.

Therefore, you would not be able to create portable C++ code - you would have to know each architecture before you started writing code!
However, C++ allows you to forget about architecture in this case - p+1 means that you go to next place in memory.
It's easiest to get when you check adress output.
For example, you first have int pointer pointing at adress, let's say, 0x0004
If you added 1 to this pointer, you add one size of an int, so it's 0x0008 (now we assume that int has 4 bytes - it doesn't have to be true, we are assuming it for demonstration purpose only).

You can check tutorials on pointers arithmetic:
http://www.tutorialspoint.com/cplusplus/cpp_pointer_arithmatic.htm
http://www.eskimo.com/~scs/cclass/notes/sx10b.html

Cheers!
Topic archived. No new replies allowed.