# Thread: 2 Questions about POINTER and MEMORY.

1. ## 2 Questions about POINTER and MEMORY.

int *p= 0;

p++ ; // why each ++, will increase the address by 2 ????

output:
0x12345670
0x12345672
0x12345674
0x12345676
.
.
...

//---------------------------------------------------------------------------//

int *p = 0 ; // That 's OK

int *p = 1 ; // Why ERROR?????

thx~!

2. p++ ; // why each ++, will increase the address by 2 ????
if you work with 16-bit adresses when you increase the adress you move it forward by 16-bits,which is 2 bytes..therefor it increases by the value of 2.

int *p=0;
this means that p points to nothing.
more apropriate might be:
int *p=NULL; but it's practically the same.
I guess your compiler doesnt like this:
int *p=1;
and it shouldn't. You'd want to initialize a pointer to an adress, not a number..

/btqq

3. Originally posted by btq

and it shouldn't. You'd want to initialize a pointer to an adress, not a number..

/btqq [/B]
how to initialize it to an address ?

p = 1 ; /// why can't i use 1 as an address??

4. the most common way to do it is to let the pointer point to a variable...

int i=2;
int *ptr=NULL; //it's a good thing to always initialize pointers to NULL....
ptr=&i;

& is an adress operator that is used to assign the adress of i to ptr..so now ptr points to the variable i, that is, ptr contains the adress in which the contents of i is stored.

there are LOTS of text on the net about this, you might wanna check it out..

/btq

5. Hi,
As far as I know you cannot say:
Code:
`int *p=1`
because if you initialize the pointer you should initialized it to as address since the pointer stores the address only,

but I think you can do this:
Code:
```int x;
int *p;

p=&x;

*p=1;```
so with *p you can access the content of the variable which address is stored in the pointer *p.

6. so with *p you can access the content of the variable which address is stored in the pointer *p.
yup, the * here is called a dereferencer..

/btq

7. Originally posted by ammar
Hi,
As far as I know you cannot say:
Code:
`int *p=1`
because if you initialize the pointer you should initialized it to as address since the pointer stores the address only,

but I think you can do this:
Code:
```int x;
int *p;

p=&x;

*p=1;```
so with *p you can access the content of the variable which address is stored in the pointer *p.

HMM , i understood , thx.

Popular pages Recent additions