pointerz

This is a discussion on pointerz within the C Programming forums, part of the General Programming Boards category; can anyone tell me were i can find a step by step tutorial on how to learn pointerz...... but something ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    pointerz

    can anyone tell me were i can find a step by step tutorial on how to learn pointerz...... but something that really is simple for a really stupid (but i mean really stupid)person like me

    thanx

  2. #2
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    See if you find this useful: Introduction to Pointers

    Good luck.
    Jason Deckard

  3. #3
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    You've been one step ahead of me all morning, vVv
    Jason Deckard

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    thanx all. if you see anymore that says pointers for comple stupid please post it here for me thanx for your help

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Some introductions to pointers:

    http://www.geocities.com/SiliconVall...cpointers.html
    http://www.ontko.com/~rayo/cs35/pointers.html

    Another really useful source is The C Programming Language from Kernighan & Ritchie. It has not only a chapter about pointers, but is a very valuable reference when programming in C.

  6. #6
    Sayeh
    Guest

    pointers explained

    To: xlordt

    Pointers are not difficult-- it's the way most people explain them, because in truth they don't grasp them either. Instead, let's start out with how memory is formed and accessed. Once you understand that, pointers become trivial:

    RAM (Random Access Memory) is like a *BIG* checkerboard. The smallest element that RAM is usually described in, is the Byte. You can work with even smaller pieces, but for now we'll deal with just the Byte.

    A Byte is like each square on the checkerboard. It has a unique, specific, and identifiable location in RAM that never changes. It holds a specific amount of information, that also never changes (eg. a 'Byte' is always a 'Byte').

    With that in mind, (that each and every Byte sits in a unique position in RAM, just as each square on the checkerboard sits in a unique spot) how do you access information in RAM?

    This is where 'pointers' come in. A 'pointer' is an _address_. So, everytime you see the word pointer, replace it with the word 'address'. Each Byte in RAM has a specific location. Since RAM is consecutive (one byte follows another, from the start of RAM until the very end), that means that the very first address in RAM is location zero. The last address in RAM is whatever location contains the last accessible Byte of RAM.

    So if you wanted to access the first Byte you would go to location 0x00000000 (zero). The next one would be at location 0x00000001 (one), and the next at 0x00000002, and the next at 0x00000003, and so on.

    Well, now you know what RAM is and how to access it, atleast conceptually. But you still have a vague idea of what a pointer is. Here's the next piece:

    In C or C++, a 'pointer' is actually just a signed long variable. The term 'pointer' was coined because the long value stored in this variable (aka the 'contents of the variable') happens to "point to" or "address" a specific location in RAM.

    This is where most programmers/coders/developers get confused.

    A pointer is a variable. The contents of the variable is the address it "points to".

    These are 2 *different* things.

    Let's look at the code:

    Code:
    char *theString;                          /* declare the pointer */
    'theString' is a variable. In most processors today, it will contain a 32-bit value. This can be positive or negative. It is always a number.

    'theString' resides at its _own_ location in RAM. This is NOT what it points to. Remember, everything you access, everything you touch, MUST reside _somewhere_ within the computer's RAM. Whether it is a variable, a constant, a function, etc.

    So, in the code:

    Code:
    theString = 0x12345678;          /* point it somewhere */
    If we assign a value to 'theString' that value must be stored somewhere in RAM, wherever the compiler decided the 'theString' variable would reside.

    Let's say that 'theString' resides at 0xABCDEF01 in RAM. Memory would look like this:

    Code:
    Address     Contents (in bytes)
    ----------  --------------------------------
    0xABCDEF01  12 34 56 78 F7 03 AB 64
    0xABCDEF09  A7 21 2D AA 64 7B 39 22
    0xABCDEF11  ...
    Note that the first four (4) bytes are our address 0x12345678. The rest is other values in RAM.

    Whenever you access 'theString', the compiler actually writes the code to go to location 0xABCDEF01 (we're ignoring link and jumptables at this stage as unimportant to the conversation) and returns the value it finds there, which in this case is: 0x12345678.

    Which is the 'address' of your 'theString' data.

    Now, what if you had another variable and you wanted the address of your pointer? Let's examine code:

    Code:
    char *thePointer;
    char *theString;
    
    thePointer = &theString;
    What would 'thePointer' contain? Think about it....



    If you said '0xABCDEF01'-- you were RIGHT.

    ------

    Okay, that explains playing with pointers. Now how do you access the data the pointers point to? That's where the '*' operator comes in.

    Whenever you see '*', with a variable, it means you want use the _contents_ of the variable to access data elsewhere.

    This is called 'indirection'. Rather than accessing the contents of RAM at some location by specifically specifying the address where the data is, you are going _indirectly_ through the contents of a second variable.

    Let's see code:

    Code:
    *theString = 13;                          /* 13 decimal is 0x0D in hex */
    What is that really doing? Well, by using the '*' we are saying okay, get the contents of 'theString' (which is 0x123456780 and go through it. Whatever byte is located at 0x12345678, will be changed to contain a 13, instead of whatever it previously held.

    Before:

    Code:
    Address     Contents (in bytes)
    ----------  --------------------------------
    0x12345678  EE 21 AA 38 63 03 1A 64
    0x12345680  9A 12 7B A7 34 7B 3D 22
    0x12345688 ...
    After:

    Code:
    Address     Contents (in bytes)
    ----------  --------------------------------
    0x12345678  0D 21 AA 38 63 03 1A 64
    0x12345680  9A 12 7B A7 34 7B 3D 22
    0x12345688 ...
    Notice how the 0xEE became a 0x0D. This is what is really going on when you work with pointers and memory.

    This is all there is to pointers. Beyond this, are merely additional levels of indirection. For example, if you were doubly dereferencing (that's indirection x 2) it means you would go through the contents of one pointer, and then go through the contents of the second location that the first pointer pointed at (treat the second one like a pointer, too).

    So using our final code example above, if we were to double-dereference off of 'theString', we would be saying the following (we'll use 'n' as a mental math variable):

    Code:
    char  n;
    
    n = **theString;                               /* original premise */
    
    
    /* what it means */
    
    n = *0xABCDEF01;                           /* first dereference means n = 0x12345678 */
    
    n = *0x12345678;                           /* second dereference means n = 0x0D21AA38 */
    Or, in other words, since 'n' is actually referencing a byte, not a long, n = 0x0D.


    Please feel free to ask more questions. Sorry it's all text, but I can't give you a drawing (which _is_ worth a thousand words in this case.)

    enjoy.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    ohh .......... all of that looks scary and hard i sure hope i can understand pointerz soon thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointerz
    By xlordt in forum C Programming
    Replies: 4
    Last Post: 04-17-2002, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21