Pointers, what are they good for... - c++

Started by
19 comments, last by Erlog 22 years, 9 months ago
And now, my 2 cents, though I think Erlog's assignment may be due already.

Pointers are a means of accessing a section of memory. Regardless of what that purpose may be -reading, writing, allocating, freeing- all a pointer is to the processer is a 32-bit unsigned value representing an offset in memory (though programmers and the compiler usually treat them quite differently).

My favorite example, the one that opened the door for me with pointers, went kinda like this:

Say you have a structure type, such that the first member of the structure is an integer, and the others are single characters. In some function, you have a local TExampleStructure variable, a pointer for this variable type, and an integer pointer, defined and initialized.

In c/c++ the structure declaration and function definition might look like this:

  struct TExampleStructure{    int   AnInteger;    char  Character1;    char  Character2;    char  Character3;    char  Character4;};  void SomeFunc( void ){    TExampleStructure   ExampleStructure = { 0x41424344,'a','b','c','d' };    TExampleStructure* pExampleStructure = &ExampleStructure    int*               pInteger          = &(ExampleStructure.AnInteger)}  


Assume that the ExampleStructure resides at memory address 4000h. pExampleStructure points to the entire ExampleStructure structure and pInteger points to AnInteger. Looking at the pointer variables in a debugger you'll see that even though they were initialzed with seemingly different variables, they have the same value (4000h).

This makes sense if you look at how the structure is allocated in memory (excluding alignment and packing).

        |------------------|------------|---------|4000h:  | ExampleStructure | AnInteger  | 4 bytes |        |------------------|------------|---------|4004h:  |                  | Character1 | 1 byte  |        |------------------|------------|---------|4005h:  |                  | Character2 | 1 byte  |        |------------------|------------|---------|4006h:  |                  | Character3 | 1 byte  |        |------------------|------------|---------|4007h:  |                  | Character4 | 1 byte  |        |------------------|------------|---------|  


They both just point to whatever is at memory address 4000h. The fact that one is an integer pointer means to interpret its contents as an integer. The TExampleStructure pointer's contents should be treated as a TExample structure.

Remember, a pointer is just a way of accessing memory. It's up to the program to interpret what is actually there.

BTW, this is a very good thread for people getting started with programming in general. I'm going to send a couple of friends here. Good stuff, guys.

Thank you for your bandwidth.
-- Succinct
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Edited by - Succinct on July 4, 2001 12:44:31 AM
-- Succinct(Don't listen to me)

This topic is closed to new replies.

Advertisement