Re: Pointer Question



"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


.



Relevant Pages

  • Re: printing a pointers value
    ... You need to make a clear distinction between the value of a pointer and ... Here you have defined zp as a pointer to an int. ... *zp tries to dereference zp i.e. access what zp is pointing at. ...
    (comp.lang.c)
  • Re: Question
    ... > Im not sure you fully understand a pointer. ... > He is pointing to that memory ... > int x; ...
    (comp.programming)
  • Re: using realloc()
    ... > with alignment in memory. ... the first function will still be pointing to the original address. ... If there is the need to increase the memory a pointer is pointing to ... int increase_p ...
    (comp.programming)
  • Re: ptr conversions and values
    ... >> An address doesn't know what object type it is really pointing at. ... That location currently contains an int, ... that int except by converting it into a pointer of a different type. ...
    (comp.std.c)
  • Re: ptr conversions and values
    ... >> An address doesn't know what object type it is really pointing at. ... That location currently contains an int, ... that int except by converting it into a pointer of a different type. ...
    (comp.std.c)