Problems

This is a discussion on Problems within the C++ Programming forums, part of the General Programming Boards category; Im confused a little bit on pointers and reference. Can somebody explain step by step what they are and what ...

  1. #1
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542

    Unhappy Problems

    Im confused a little bit on pointers and reference. Can somebody explain step by step what they are and what they are used for?
    Thanks
    what does signature stand for?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    pointer = (*) a memory address.

    reference = (&) a memory address (but it looks like the variable itself)

    The following code changes the same variable to 0 and then to 1 in the function.
    Code:
    void func(int *ptr, int & ref)
       {
       *ptr = 0;
       ref = 1;
       }
    
    int main( void )
       {
       int test;
       func(&test, test);
       return 0;
       }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Lol. I know the code to use them. But I want to know the purpose of them etc
    what does signature stand for?

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    well, I thought it seemed like an odd question. You had posted similar things in the past. ok...

    You usually won't just have a pointer to an int as in the example. A lot of times it'll be a pointer to a struct or class. If you didn't do a pointer and you just passed it in, you couldn't modify it for one, and also you would be making a copy of it. In some cases this can be very painful.

    Also, you may be using a variable in the middle of a large buffer. You can just find the place in the buffer with a pointer and then use that small part of the buffer to pass in.

    I guess if this is not what you're looking for then I'm not sure what you're asking.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    if you're not sure why there is a difference between reference and pointer then I would say that reference is just a new convenience added for C++. It's kinda the same thing.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Yea.. first of all... what IS a pointer and what is it USED for?
    what does signature stand for?

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    hmm.... are you being serious? it's kinda hard to tell.

    if memory is a huge block of 0's and 1's and you have some chunk of it you want to use, there will be an offset from the beginning of that huge block. This is called the address. From this address you can read, change etc... the memory. Variables are nothing more than pieces of this block and pointers are nothing more than the address in the block.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Ok. What are pointers used for?? Is it just to read memory??
    what does signature stand for?

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    well yeah, kind of a guarantee that you're talking about the exact same chunk of memory.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Why do programs use it??
    what does signature stand for?

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ???

    they use it so they can access a chunk of memory!

    You want an example or something? how bout this..... A binary tree has node of this type:
    Code:
    class Tnode
       {
       public:
          Tnode *left;
          Tnode *right;
       };
    non pointer data and functions are left out.
    the pointers are pointing to the same type node. when you are building the tree, you would not know how big and where the branches should be so they need to be created at runtime. Then you fill in the nodes:
    Code:
    node->left = new Tnode;
    Like that. That's just one example. There are tons of reasons to use pointers. How 'bout across dll's? if you have a dll that you want to pass some large structure into you would probably want to pass a pointer:
    Code:
    DLLFunction(&mystruct);
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    What's a binary tree?
    what does signature stand for?

  13. #13
    Registered User
    Join Date
    Aug 2002
    Posts
    16
    pointers and references are the C++ equivalents of the cpu's direct and indirect addressing.

    a reference is the hard-coded position of the data in memory. This will either be relative to the stack (for local variables) or an absolute value somewhere in your address space.

    a pointer is a variable 32 bits in size that basically contains what a reference does. It is thus an indirect addressing mode.

    Pointers are handy because you can name them and change their value. You can have a pointer point to one struc, and then assign it to another. You can walk a pointer along a string or an array of structures. You can modify pointers at run time

    References, because they are hard-coded, cannot be changed at run time. They point exactly to that object (and in HLLs only to that object).

    Hope that clarifies things.

    --Chorus

  14. #14
    Registered User
    Join Date
    Aug 2002
    Posts
    16
    binary trees are like apple trees. But they have binaries on them instead of apples

    --Chorus

  15. #15
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    That's good! Keep them coming
    what does signature stand for?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM

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