Pointers

I'm having trouble understanding what pointers are. I looked on another site, and it made me think that all they are is the property of substition, in geometric terms. Like here is an example of a pointer (I think):

x=2
y=x+2

Is variable y a pointer? If not, what would be an accurate description and example of a pointer?

One last question: is there anything more to pointers that I do not know? (based on what I just told you)
Try reading this: http://www.cplusplus.com/doc/tutorial/pointers/

Then ask about what you did not understand in the document.
What Mats said still stands.

A pointer is a type of variable that contains a memory location rather than, say, a number, character or more complex structure. You can think of pointers as just that; variables that point to some information rather than containing it themselves. Pointers are a big topic, and you really need to read the literature.
To be metaphorical:
A pointer is rather like finding a number on a piece of paper to another locker in your locker in a very large school.
The first locker(the pointer) is the memory space that contains the information (number on paper) that tells you where the information is stored (in the locker that the paper is referring to).

Hope the metaphor helped...
closed account (zb0S216C)
A pointer is a variable in itself. It's value is an address in memory, as well as its address. In C & C++, the pointed-to address must contain the type that corresponds to the type used to declared the pointer. For example, a pointed declared as a char can only point to an address that stores a char.

The size of the pointer is implementation dependent. The size of a pointer can be any size so long as the size can store the highest address in memory. Usually, and most commonly seen, sizes are 4 bytes (32 bit architecture) and 8 bytes (64 bit architecture).

Pointers are special and require different treatment from regular variables. Every address contains a value; be it "garbage" or a value used by another program. When a pointer points to another address, the contents of the address is assumed to be the same type as the pointer.

To acquire the contents of the pointed-to address, you must de-reference the pointer; that is, "look into the address pointed-to by the pointer". C++ will take the contents of the address and interpret it accordingly. Note that every operator is a function in disguise; the asterisk is no exception. When a pointer is de-referenced, the contents of the pointed-to variable is returned to the calling function.

A non-de-referenced pointer is the pointer itself. A de-referenced pointer is the pointed-to variable, not the pointer.

Assigning a pointer to a variable requires the explicit passing of the right-hand operand's starting address to the pointer. Null can be assigned to a pointer which indicates that the pointer isn't pointing to anything.

When void is used to declare a pointer, the pointer can point to anything. However, a decent compiler won't let you write to the pointed-to address. Why? Because the contents and size (the size of the region) of the pointed-to address is unknown and C++ doesn't know how to handle such as request.

Wazzak
Last edited on
Topic archived. No new replies allowed.