Elementary C++ Conceptual Question

Discussion in 'Programming General' started by ET Phone Home, Jul 10, 2010.

Elementary C++ Conceptual Question
  1. Unread #1 - Jul 10, 2010 at 11:21 PM
  2. ET Phone Home
    Joined:
    Sep 8, 2008
    Posts:
    663
    Referrals:
    1
    Sythe Gold:
    0

    ET Phone Home Apprentice

    Elementary C++ Conceptual Question

    Hi.

    I don't quite understand referencing things, dereferencing things, and pointers

    if you say

    int one = &two;

    int one will then have the value of the location/memory address of int two?

    and if you say

    int * one = two

    int two will then have the value of whatever content is in the memory address of "int one?"

    How is any of that useful? I'm extremely new to c++ and I'm not really grasping how this memory address/memory location value is useful?


    And can anyone define what a pointer is and does, in layman's terms?
     
  3. Unread #2 - Jul 11, 2010 at 6:59 AM
  4. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    Elementary C++ Conceptual Question

    To be honest, I too still get confused with pointers. It gets more useful with big programs and passing values around. It allows you to work with the original value, instead of a copy.

    Here is an example:

    You have a class named car.

    Say you want to pass an object instance into a function.

    Code:
    void myFunction(Car myCar) 
    {
    ..do stuff..
    }
    
    When doing that, it would make a copy of the object instance, taking up more space in memory. Now, this would is avoidable with pointers.

    If you are editing the original values in the object, then you can pass it by reference and anything you change in the class, will be changed in the original object.
    If you don't want to change any values in the object, you can pass it by reference, but have it as a constant.

    Say you want to pass an object instance into a function. You should do so like this:

    Code:
    // In this function we will want to edit values in myCar
    void myFunction(Car &myCar) 
    {
    ..do stuff..
    }
    
    Code:
    // In this function we will not be editing any values in myCar
    void myFunction(const Car &myCar) 
    {
    ..do stuff..
    }
    
    With those methods, you don't make entire copies, just pass the memory locations of the original object. In bigger programs this is very useful for increasing performance.

    ------

    As for your other question regarding:

    You are right.

    "&" basically means address of. You are assigning the memory address of that variable to another. Meaning both variable point to the same address in memory where your value is stored. When modifying either variable, the value at that address will change in both variables since they point to the same spot.

    when using pointers, *pointerVar (dereferenced) contains the value at the address, where just using pointerVar, holds the memory address. So you can assign pointers in two ways.

    Code:
    int one = 3;
    int *pointerVar;
    
    pointerVar = &one; // This assigns the address of variable one, to poitnerVar.
    *pointerVar = one; // This also will assign the address of the value to pointerVar
    
    In either of these, if pointerVar is modified, so will one.

    Sorry if my explanation sucks, If you need a clearer explanation let me know, or if you have further questions.
     
  5. Unread #3 - Jul 11, 2010 at 9:34 AM
  6. ET Phone Home
    Joined:
    Sep 8, 2008
    Posts:
    663
    Referrals:
    1
    Sythe Gold:
    0

    ET Phone Home Apprentice

    Elementary C++ Conceptual Question

    great explanation, i think i was getting confused with dereferencing vs. pointers, mixing up the asterisks lol. ill try to make some simple programs with them to get a better idea of their behaviors probably
     
  7. Unread #4 - Jul 21, 2010 at 12:51 AM
  8. aznguy94
    Joined:
    Feb 11, 2007
    Posts:
    304
    Referrals:
    0
    Sythe Gold:
    0

    aznguy94 Forum Addict

    Elementary C++ Conceptual Question

    If you really want to learn pointers, look up Linked Lists and Binary Trees to start; that's where pointers start to get useful in C/C++.

    Pointers give you more control over your memory which is why C++ is so much more useful/dangerous than Java.
     
< Fresh And Valid Ccv Fullz Here | First guess my number program >

Users viewing this thread
1 guest


 
 
Adblock breaks this site