If I understand right, pointers are used to point a variable to the location of another variable's memory address, essentially setting the variables equal together right?
If so, what's the point? Why not just set the variables equal together? Is there something I'm missing?
Pointers?
Page 1 of 16 Replies - 458 Views - Last Post: 05 February 2009 - 10:50 PM
Replies To: Pointers?
#2
Re: Pointers?
Posted 05 February 2009 - 09:19 AM
I'm actually rushing out of the door, so I'll try and make this quick.
Do you know about what references are? Pointers and references are the same thing, except that pointers don't do the behind the scenes work for you.
Aside from that, a pointer can allow you access to a variable that doesn't "exist" in a particular function you are using, but it's best for when you are working with dynamically allocated memory, and for more complex structures like linked lists and binary trees.
If you look at the code, yes, you'd probably agree that this is implemented easier with arrays, but linked lists have certain advantages over arrays when it comes to removing items from the list, or moving them around.
It's pretty crazy, because when you first learn about pointers, most people think "what's the point about this?" But then, you don't truly understand why you need them until you're dealing with operations that are more complex. Unfortunately, pointers are one of those things you MUST learn in order to be a good programmer, no matter how pointless they may seem now. The code example that I shared doesn't even begin to touch their usefulness, but will show when you start writing larger programs.
I will say this, however. The ability to know the address of any variable, or any other block of allocated memory is extremely power. However, with this power, holds great responsibility.
I'm hoping the code sample helped.
Do you know about what references are? Pointers and references are the same thing, except that pointers don't do the behind the scenes work for you.
Aside from that, a pointer can allow you access to a variable that doesn't "exist" in a particular function you are using, but it's best for when you are working with dynamically allocated memory, and for more complex structures like linked lists and binary trees.
#ifndef __cplusplus
#error "compile with c++"
#endif
#include <cstring>
#include <cstdlib>
using namespace std;
struct linkedListNodeInt
{
int value;
// This points to the next "node" in the list.
// This is synonymous to the next element in
// an array.
linkedListNodeInt *next;
linkedListNodeInt(int val) : value(val), next(NULL) {}
linkedListNodeInt() : next(NULL) {}
};
int main(void)
{
linkedListNodeInt *lli = new linkedListNodeInt(57);
linkedListNodeInt *curr = lli;
for (int x = 0; x < 5; x++)
{
curr->next = new linkedListNodeInt(rand()%100);
curr = curr->next;
}
curr = lli;
while (curr)
{
cout << curr->value << endl;
curr = curr->next;
}
// Be sure to deallocate the array to avoid a memory leak.
curr = lli;
while (curr)
{
lli = lli->next;
delete curr;
curr = lli;
}
}
If you look at the code, yes, you'd probably agree that this is implemented easier with arrays, but linked lists have certain advantages over arrays when it comes to removing items from the list, or moving them around.
It's pretty crazy, because when you first learn about pointers, most people think "what's the point about this?" But then, you don't truly understand why you need them until you're dealing with operations that are more complex. Unfortunately, pointers are one of those things you MUST learn in order to be a good programmer, no matter how pointless they may seem now. The code example that I shared doesn't even begin to touch their usefulness, but will show when you start writing larger programs.
I will say this, however. The ability to know the address of any variable, or any other block of allocated memory is extremely power. However, with this power, holds great responsibility.
I'm hoping the code sample helped.
This post has been edited by thepeoplescoder: 05 February 2009 - 09:25 AM
#3
Re: Pointers?
Posted 05 February 2009 - 09:28 AM
IrishCereal, on 5 Feb, 2009 - 09:58 AM, said:
If so, what's the point? Why not just set the variables equal together? Is there something I'm missing?
What if the variable is 50K, do you want to make a copy? Or 50M?
Pointers make more sense when you start using functions. I pass a list to a function so it can add an item. If I pass a copy, it can only change the copy and I get nothing of value returned. If I pass a pointer, it changes the list, which is what I wanted.
#4
Re: Pointers?
Posted 05 February 2009 - 09:36 AM
IrishCereal, on 5 Feb, 2009 - 03:58 PM, said:
If I understand right, pointers are used to point a variable to the location of another variable's memory address,
IrishCereal, on 5 Feb, 2009 - 03:58 PM, said:
essentially setting the variables equal together right?
Pointers, however, do exist at runtime (they're stored as objects in memory), so they will allow you to keep track of particular areas of memory, at runtime.
IrishCereal, on 5 Feb, 2009 - 03:58 PM, said:
If so, what's the point? Why not just set the variables equal together? Is there something I'm missing?
Since pointers are a runtime thing, they don't care about 'scope' in the same way that variables do - so you can pass a memory address as a function argument, and modify a memory location which (from the compiler's perspective) doesn't necessarily belong to that function's scope.
(in C++ its common to use references to do this - a reference is like a variable which doesn't own any memory, it works as an alias, or nickname, for some other memory)
if you're making a program where your requirements go beyond simple variables, then pointers become far more useful
This post has been edited by Bench: 05 February 2009 - 09:50 AM
#5
Re: Pointers?
Posted 05 February 2009 - 03:58 PM
Thank you all! I think I understand now. I was talking to my Computer Programming teacher at school and he said some of the same things. Thanks a bunch!
#6
Re: Pointers?
Posted 05 February 2009 - 07:00 PM
I agree completely with Baavgai and Bench. I didn't really get pointers because my book covered them before objects. Its easy to pass a tiny number, but other things get passed around, like objects, that you want to be able to pass with a pointer than by the whole thing itself.
#7
Re: Pointers?
Posted 05 February 2009 - 10:50 PM
:: pointers are effective in terms when you are using huge structures,
:: 1. lending address instead of copying (copy will consume memory)
:: 2. when you change a copy the original won't change
:: " kill him .. don't confuse him with his illusion "
:: 3. address will lend you performance and access
:: if i have objects .. then a pointer of their base would be a supervisor,
:: to search objects of same class you can't go internally !
:: but using a pointer that would increment illogically to seek,
:: good luck with pointers
:: 1. lending address instead of copying (copy will consume memory)
:: 2. when you change a copy the original won't change
:: " kill him .. don't confuse him with his illusion "
:: 3. address will lend you performance and access
:: if i have objects .. then a pointer of their base would be a supervisor,
:: to search objects of same class you can't go internally !
:: but using a pointer that would increment illogically to seek,
:: good luck with pointers
This post has been edited by Plus: 05 February 2009 - 10:51 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|