Re: Pointer Question
- From: "Daniel Ranger" <dranger003@xxxxxxxxx>
- Date: 30 Apr 2006 11:58:11 -0700
It does not assign "Hello" to the pointer - a pointer holds an address.The second example assigns to the pointer itself, making it point to
something valid.
char *pChar = "Hello";
So this assigns "Hello" to the pointer... but I thought a pointer just held
the 'address' of something, not an actual value?
In this case, pChar points to the address of "Hello", because "Hello"
is a char array.
In that case, why cant you do this -This is illegal. If you would really want to set *pInt, you would have
int *pInt = 10;
to write it this way:
int *pInt = (int *)10;
This is legal, but will result in having a pointer pointing at address
10. Before using memory you should always make sure it is assigned
first (heap or stack):
int *pInt = new int; // Don't forget to delete pInt when you're done
with it
// or
int value; // value has valid allocated memory but an undefined value
int *pInt = &value;
// then
*pInt = 10;
pInt is now pointing to a valid memory location and that memory
location contains the value 10.
Regards,
Dan.
.
- References:
- Re: Pointer Question
- From: Igor Tandetnik
- Re: Pointer Question
- Prev by Date: Re: Pointer Question
- Next by Date: Re: Pointer Question
- Previous by thread: Re: Pointer Question
- Next by thread: Re: Pointer Question
- Index(es):
Relevant Pages
|