Archived

This topic is now archived and is closed to further replies.

Ekim_Gram

I hate pointers and references!

Recommended Posts

Right now I''m going through Sam''s Teach Yourself C++ in 21 Days, of course if I did a lesson a day I''d be done by now but I chose to take it slow. I''m up to Day 11: Objectr-Oriented Analysiss and Design but I''m still confused out of my mind about references and pointers. I mostly understran references, I just don''t like them much, I''m guessing I will in time, though. But pointers, uggh, I hate them so much. Can somebody just give me a simple explanation of what they are and what they do? I''ve gone through the pointers chapter about 3 times and I''m gonna go over it again and again, each time I do I understad a little bit more but I just hate them. I know that they are variables that hold memory addresses. But can anybody gimmie an explanation in "dumbass" terms. I''m no dumbass but seriously, that could be the only way I''d be able to get this stuff. There''s no town drunk here, we all take turns.

Share this post


Link to post
Share on other sites
there are plenty of tutorials about pointers on this site...do a search.

Everyone hates pointers when they first start, but then grow to love them.

Share this post


Link to post
Share on other sites
That's it. They're variables that hold a memory address. As for their uses, don't worry about them. Pointers are absolutely essential to most programming problems, and you will find more and more uses for them when you learn more advanced topics.

EDIT: I've never hated pointers. They are very, very simple, if aparrently useless at first. I think it's usually their seeming uselessness that mekes people hate them, not real confusion about what they are. The best thing to do is just to forget about it for the moment. Learn how to use them (not too hard), and you'll find out the why soon enough.

[edited by - micepick on May 1, 2003 7:36:32 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Ecko
there are plenty of tutorials about pointers on this site...do a search.

Everyone hates pointers when they first start, but then grow to love them.



I don''t need tutorials, I just can''t understand them. Maybe your right though, I just gotta wait and see what this book brings me. *Sigh*


There''s no town drunk here, we all take turns.

Share this post


Link to post
Share on other sites
I agree with Ecko,

U start out with hating them, then u grow to love them..
:D

I would recommend to NOT continue with the chapters if you DONT understand pointers..

If you do understand pointers, but hate them or dont see why they are useful then continue with the chapters..

But yeah basically a pointer is a variable that hold memory adresses. If you understand that, then continue with the book.

Share this post


Link to post
Share on other sites
A variable is a memory location. When you store something into a variable (int i = 10; ) the value is stored in that memory location (okay, ya probably already got that much figured out). A pointer, on the other hand, holds a memory address. This address can be changed. For instance, if you were to say "int *ptr = &i", you are taking a reference to "i" (in other words, the memory location of the "i" integer that we declared earlier) and storing it in "ptr". Now, when we access "ptr" we are really accessing "i". However, we can change "ptr" to allow us to access a different variable. For example, "ptr = &j;" stores the memory location of "j" into "ptr". Now when we access "ptr" we are actually accessing "j".

Perhaps you don't see the purpose of this yet, but just trust that there is a use for it. Eventually, you're going to run into a problem and you'll say, "Hey, I can use a pointer for that!" Just be patient with it.

- Jay


[ Here, taste this ]


[edited by - coderx75 on May 1, 2003 7:48:44 PM]

Share this post


Link to post
Share on other sites
Right now you know enough about pointers, they hold memory addresses. As long as you know how to create them and derefrence them then move on.

Most ppl who start learning pointers don''t have a hard time implementing them but why they are there. They hold memory adrresses so what. After a few chapters youll learn why and when to use em. Goodluck.

BTW 90% what I said Micepick has also stated, listen to him.

Jeff D

Share this post


Link to post
Share on other sites
Hi Ekim_Gram,

Maybe you could be a bit more specific in your questions. Fine, we know that you don''t understand pointers, but that''s quite a general statement. You''ve already said there''s some stuff you understand. Perhaps if you skim through the chapter in the book again, and pick out the bits that confuse you, and post them here. A more specific question will get you a more specific answer.

Also remember that sometimes you have to come across a real-life problem before you''ll find a use for a particular feature.

Share this post


Link to post
Share on other sites
I also am currently working with pointer*.[new to programing]
From what i gather it goes like this.Rather than your program
having to make a copy of a variable,store it then copy it back.
wich produces unwanted overhead and slows your program down.
you can use pointers* to dereference (<- fancy name for accessing the info stored at that memeory address).Also the
pointers* must be of the same type.If you declare a pointer* of
type double- you have to point it to a type of double.I hope
this help.Anyone feel free to correct me if im wrong[as i am
only on chapter 6 myself ]

Also if you understand how to write them but are unsure of the
details of pointers*,skip and keep reading.Sometimes things will
become more clear as you read on.(i will usualy read the book
twice)
big

Share this post


Link to post
Share on other sites
Pointer access is slower than "normal" access in most situations
You will need pointers for dynamic memory. References are useful when you want to pass large objects to functions.

Share this post


Link to post
Share on other sites
I always find that if I''m really struggling to understand something, it helps to see/read it explained by in different ways so DO try looking up some tutorials, or chapters on pointers from other C/C++ books you may have. You may just come across an explanation that makes it suddenly become clear.

Caroline M.

Share this post


Link to post
Share on other sites
I agree pointers are a subtly confusing topic. The more you work with them, the more you realize they can do for you.

A pointer points to something else. It's that simple.

If I make a "pointer-to-an-integer" variable (int*), then the pointer points to an integer somewhere else in memeory. If I make a "pointer-to-a-Foo object" variable (Foo*), then the pointer points to a Foo object somewhere else in memory.

I hope this snip of code will help you, study it and see if you can see how a pointer makes it possible to do different things (code below only hints at some of the power of pointers).


  
class Foo {
string name;
public:
Foo(const string& _name) : name(_name) {}
// some big, honkin' object

void log() { cout << "Foo object " << name << endl; }
};

void main() {
Foo foo1("Foo1"); // a Foo object variable

Foo foo2("Foo2"); // another Foo object variable


// call the log functions

foo1.log();
foo2.log();

cout << "address of foo1 is " << &foo1 << endl;
cout << "address of foo2 is " << &foo2 << endl;

Foo* pfoo = 0; // a pointer-to-Foo variable, set to NULL


// point pfoo to the foo1 object:

pfoo = &foo1;

// now pfoo points to the foo1 variable:

cout << "Foo object that pfoo points to starts at " << pfoo << endl;

// call the log() function for the object that pfoo points to

pfoo->log();

// now point pfoo to another Foo object

pfoo = &foo2;

// now pfoo points to the foo2 variable:

cout << "Foo object that pfoo points to now starts at " << pfoo << endl;

// call the log() function for the object that pfoo points to

pfoo->log();

}


Regards,
Jeff

Edit: Used source tags instead of code tags

[edited by - rypyr on May 2, 2003 12:08:06 PM]

Share this post


Link to post
Share on other sites
"References are useful when you want to pass large objects to functions. "
Aren''t they also useful if you want to modify a variable within a function, my understanding is, without passing by reference you only get a copy of the variable so any modifications you would try to make will only affect that variable and not the original. Is that correct?

Share this post


Link to post
Share on other sites
quote:
Original post by deadalive
"References are useful when you want to pass large objects to functions. "
Aren''t they also useful if you want to modify a variable within a function, my understanding is, without passing by reference you only get a copy of the variable so any modifications you would try to make will only affect that variable and not the original. Is that correct?


Your understanding is correct. References are also good for making "output" parameters to a function (i.e. if you want to return multiple values from a function):

i.e. something like this:

bool filterList(const List& inList, const string& filterStr, List& outList);

would allow you to write this:

List BigList = ...
List FilteredList;
if(filterList(BigList, "Blah", FilteredList)) {
cout << "Correctly filtered list for Blah" << endl;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Pointers are useful because using pointers your game will run faster than no using them.

Share this post


Link to post
Share on other sites
I know what you mean. I just learned pointers myself not to long ago. I understand them pretty well but I hate them. I haven''t gotten to the point where they''re useful yet so I guess thats why. I guess I''ll grow to love them but it sure is taking awhile.

Share this post


Link to post
Share on other sites
i never really ''hated'' pointers. they were confusing in the very very beginning. they are much more useful than you think. if it wasnt for pointers you could not use function pointers (you''ll learn what they are later) or polymorphism (i think thats spelled right). here are a few examples of pointers:

  
#include <iostream>

// base class for other states

class State
{
public:
// declared virtual so that

// when you try to access

// it with a State pointer

// it will call the update

// function of the child

// class the pointer points

// to

virtual void update()=0;
};
// game class

class GameState:public State
{
public:
void update(){cout<<"called GameState::update\n";}
};
// title screen class

class TitleState:public State
{
public:
void update(){cout<<"called TitleState::update\n";}
};

int main()
{
// start out as title screen

State* currentGameState=new TitleState;

// calls update function of TitleState

currentGameState->update();

// release memory

delete currentGameState;

// create a game class

currentGameState=new GameState;

// calls update function of GameState

currentGameState->update();

// keeps the window from closing when

// program is done

cin.get();

return 0;
}

that should compile. the output should be:
called TitleState::update
called GameState::update



doh, nuts. Mmmm... donuts
My website

Share this post


Link to post
Share on other sites