OK...so I'm reading my C++ book and I'm at the point (no pun intended) however where I am reading about pointers. I'll be the first to say...
I'm retarded
Now that being said I'm a tad bit confoozeled by the whole pointers deal and would just like some "Ok, here is a round hole and here is a square peg" explanation on this.
A pointer is exactlly as it says...it just points at another variable right? (As I explain my understanding you'll understand why I hate the book I'm reading).
Basically if you wanted to stick a int into a var that is currently being used at a seperate function you could use a pointer and "shove" the int into the pointer and it will be just like using that var in the first place right?
Maybe I am a little too early in this book and a little too "introduced" to Visual languages to understand why but why can't you just use Global variables?
Or maybe my retardation has done got the best of me and my brain is trying to eat itself alive? Thanks for your help.
-
-
Cybermagellan wrote:
A pointer is exactlly as it says...it just points at another variable right? (As I explain my understanding you'll understand why I hate the book I'm reading).
Basically if you wanted to stick a int into a var that is currently being used at a seperate function you could use a pointer and "shove" the int into the pointer and it will be just like using that var in the first place right?
Maybe I am a little too early in this book and a little too "introduced" to Visual languages to understand why but why can't you just use Global variables?
Or maybe my retardation has done got the best of me and my brain is trying to eat itself alive? Thanks for your help.
It's been a long long time since I did C, so everyone can have a laugh when I mess up.
I've always considered pointers to be an address in memory, not a pointer to a variable. Why? Pointer artithmatic. You can add onto a pointer and send it through an array, so it may not be pointing at a varible per se.
Why not use globals? Why not use globals in any language?
Why does ByRef exist in VB? (Err, it is byref, right?)
-
Ok this may sound funny or stupid but this really is a good video that will explain pointers and there usefulness.
"Binky Pointer Fun Video"
http://cslibrary.stanford.edu/104/
-
Using global variables is bad programming practice. The more code that has access to that variable, the more likely you are to make a mistake using it.
-
IRenderable wrote:Ok this may sound funny or stupid but this really is a good video that will explain pointers and there usefulness.
"Binky Pointer Fun Video"
http://cslibrary.stanford.edu/104/
Ok then...um. yeah it explained more than this whole chapter in this book did in 3 minutes. Do they have "Binky C++ Fun Video"? where he teaches the entire C++ language?
OK, so in a way I was right in my first post then from what I'm gathering? -
Cybermagellan wrote:
Ok then...um. yeah it explained more than this whole chapter in this book did in 3 minutes. Do they have "Binky C++ Fun Video"? where he teaches the entire C++ language?
One book I found useful when learning C was "Enough rope to shoot yourself in the foot". It covers C++ as well. It is not a tutorial, it's a list of all the mistakes you will make (and you will)
-
There's a good reason modern languages like .NET, Java doesn't have pointers. Ya don't need it. Let the compiler do the work for ya.
-
A pointer is a memory address. Nothing more.
Let's say you have a C++ object in memory located at address 12345. A pointer to that object records the location of that object in memory. So, it's said to "point to" the real object.

On a 32-bit computer, the pointer is an integer with the value 12345. The pointer acts as a reference to an object, or a handle. Often these handles are passed around rather than the objects themselves, as it's more efficient. A pointer takes up much less stack space than an entire object.
If you tell the compiler to dereference that pointer, you have the object again.
myMethod()
{
MyObject obj; // an object applocated on the stack. The variable obj is teh whole object, by value
MyObjectRef *ref = &obj; // ref is a pointer to obj. The & operator retrieves the location in memory of obj.ref->method(); // dereferencing the pointer ref yields obj. You can use "arrow" syntax to do this.
// example using integers
int x = 6; // simple int variable
int *y = &x; // pointer to x
printf("This is the value of x: %d, which is located at %p in memory\n",*y,y);
};
-
Cairo wrote:A pointer is a memory address. Nothing more.
No, it isn't. It looks a lot like a memory address, smells a bit like a memory address but it isn't a memory address. It has a type and hence a size, which affects the way pointer arithmetic works.
-
AndyC wrote:

Cairo wrote:A pointer is a memory address. Nothing more.
No, it isn't. It looks a lot like a memory address, smells a bit like a memory address but it isn't a memory address. It has a type and hence a size, which affects the way pointer arithmetic works.
Yeah, I over-simplified.
-
men, I was about to try and explain pointers... but really that binky video is awesome

Pointers are like directions to somewhere...
Think of one of your friend's house. That house has an adress, right ? that house has some content, meaning, people live there right?
So when you set a pointer , you set the directions for that house...
you want to know what's inside the house ? use *
you want to know the adress of the house ? use &
The thing with pointers is that they need to point to somewhere valid. To reserve space to build a house...you need to buy that space... that's what malloc does... it just buys you some space.
If you give me an adress for a house that's not there ... invalid
it's not complicated... but don't worry, it's neither intuitive at first
little example:
this is how you declare a pointer: house* direction;
let's buy a house... so we need to buy space for the house remember ? direction = (house*)malloc(sizeof(house))
wow... what was that?
malloc reserves space remember? the big question is... how much space ? that's what sizeof tells it to... the size of a house. the (house*) part is one you don't need to bother... just substitute house with whatever variable type you need
want to know what's inside of a house ?
insideHouse = *house;
want to know what's the adress of that new house?
adress = &house;
simple isn't it
sorry that I didn't explain it as well as binky tough 
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.