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
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?
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
Lol. I know the code to use them. But I want to know the purpose of them etc
what does signature stand for?
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
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
Yea.. first of all... what IS a pointer and what is it USED for?
what does signature stand for?
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
Ok. What are pointers used for?? Is it just to read memory??
what does signature stand for?
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
Why do programs use it??
what does signature stand for?
???
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:
non pointer data and functions are left out.Code:class Tnode { public: Tnode *left; Tnode *right; };
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:
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:node->left = new Tnode;
Code:DLLFunction(&mystruct);
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
What's a binary tree?
what does signature stand for?
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
binary trees are like apple trees. But they have binaries on them instead of apples
--Chorus
That's good! Keep them coming
what does signature stand for?