• Log in or sign up for an account here: https://www.sythe.org/login

Elementary C++ Conceptual Question

ET Phone Home

Apprentice
Joined
Sep 8, 2008
Sythe Gold
0
Messages
663
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?
 
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:

int one = &two;
int * one = two

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.
 
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
 
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.
 
Runescape Gamblingrunescape gambling
Back
Top