Re: Pointer Question
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Sun, 30 Apr 2006 14:07:57 -0400
"David++" <David@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:D1FC7B0C-36D1-483C-87F4-A73B176EFF2A@xxxxxxxxxxxxx
I am reading over Pointers again to make it clearer. In my book it
rightly says that using an uninitialized pointer is dangerous i.e.
int *pInt;
*pInt = 12;
In other words, pInt doesnt point to anything known, it could be
pointing at any piece of memory.
But in C style strings the following is used all the time to hold
character strings -
char *pChar = "Hello World";
Isnt that the same thing? as using an uninitialized ponter? I mean,
pChar isnt pointing to anything known either, is it?
The first example assigns to the memory the pointer is pointing to.
Since the pointer is uninitialized, it points to random piece of memory
which the assignment overwrites.
The second example assigns to the pointer itself, making it point to
something valid.
Consider:
int x = 0;
int* pInt; // uninitialized pointer
*pInt = 1; // undefined, illegal
pInt = &x; // well defined, legal. pInt now points to x
*pInt = 1; // now it's well defined, equivalent to x = 1;
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
.
- Prev by Date: Re: KB 839136 is wrong -- bug not fixed in VC6SP6
- Next by Date: Re: Pointer Question
- Previous by thread: Re: KB 839136 is wrong -- bug not fixed in VC6SP6
- Next by thread: Re: Pointer Question
- Index(es):
Relevant Pages
|