Re: Pointer Question

Tech-Archive recommends: Fix windows errors by optimizing your registry



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?

It does not assign "Hello" to the pointer - a pointer holds an address.
In this case, pChar points to the address of "Hello", because "Hello"
is a char array.

In that case, why cant you do this -

int *pInt = 10;

This is illegal. If you would really want to set *pInt, you would have
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.

.



Relevant Pages

  • Re: Is this math test too easy?
    ... > communications glitch; one of the more laughable cartoons ... it was loaded into physical memory and, ... > Or one can interpret the character string as one of the values ... A pointer to an integer? ...
    (sci.math)
  • Re: grow list by tail, pointer example recipe -- please comment
    ... manufacturing a pointer with that address. ... the next cons cell. ... believe these lists are in consecutive memory locations. ...
    (comp.lang.lisp)
  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)
  • Re: "Mastering C Pointers"....
    ... all means go ahead and dive right into the C language. ... Memory is a separate unit which just stores bits. ... A pointer at the hardware level _is an integer_. ... since loops make your logic more much ...
    (comp.lang.c)
  • Re: what is the purpose of C++ smart pointer
    ... pointer tracks the data it is referring to and updates itself ... following the changes of the memory it points to. ... How exactly will the smart pointer know that you moved the ... int * x = new int; ...
    (comp.os.linux.development.apps)