# Regarding Pointer

• 09-09-2009
subhashish1213
Regarding Pointer
Hi Friends,

Can we initialize a non-zero value to a pointer variable?

Ex : int *p=10;

• 09-09-2009
tabstop
Sort of. You can do that, and it will make the pointer point to the memory address 10, which you don't own; thereafter, any attempt to use the pointer will result in disaster (unless you first make it point to somewhere "real").
• 09-09-2009
subhashish1213
Regarding Pointer again!!!!
Thanks Friend!!!

Also I want to know 2 more things..

Can i declare like this:

1) int *p = 0; // I guess p here would be a NULL pointer.

2) int *p, q;
p = &q;
p=4; // What will p contain?Is it a valid statement?

• 09-09-2009
Mario F.
Quote:

Originally Posted by tabstop
Sort of. You can do that, and it will make the pointer point to the memory address 10

Sorry, but no. You can't implicitly convert int to int*. You have to make an explicit conversion:
Code:

```int* p = (int*)10; or int* p = reinterpret_cast<int*>(10);```
but you can do:

Code:

`int* p = new int(10);`
But this is a different matter.

0 (zero) is the only literal constant allowed to be assigned to a pointer type.
• 09-09-2009
anon
Quote:

p=4; // What will p contain?Is it a valid statement?
Again you are trying to assign an integer to a pointer. p is a pointer, q is an integer (but avoid declaring a mixture of pointers and other types on a single line), so the first two lines are fine.

What you can do is:

Code:

`*p = 4;`
This means, set the value of the int that p points to (in this case q) equal to 4.