6 Replies - 583 Views - Last Post: 05 February 2009 - 10:50 PM Rate Topic: -----

#1 IrishCereal  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 11-December 08

Pointers?

Post icon  Posted 05 February 2009 - 08:58 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Pointers?

#2 thepeoplescoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 97
  • Joined: 02-February 09

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.


#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

Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5822
  • View blog
  • Posts: 12,675
  • Joined: 16-October 07

Re: Pointers?

Posted 05 February 2009 - 09:28 AM

View PostIrishCereal, 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.
Was This Post Helpful? 1
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Pointers?

Posted 05 February 2009 - 09:36 AM

View PostIrishCereal, 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,
Not necessarily just another variable's memory address.. you can have memory allocated in your program which doesn't have a variable associated with it.

View PostIrishCereal, on 5 Feb, 2009 - 03:58 PM, said:

essentially setting the variables equal together right?
No, your pointer is being used to store a memory location at run time. A variable is just a name (an "identifier") which exists only in your un-compiled code to keep track of memory - i.e. variables cease to exist at runtime, because names are stripped out of your compiled program.

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.


View PostIrishCereal, 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?
if you're making a simple program where you don't care about keeping track of memory locations, then you can copy data around, and everything will probably work fine.

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

Was This Post Helpful? 1
  • +
  • -

#5 IrishCereal  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 11-December 08

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!
Was This Post Helpful? 0
  • +
  • -

#6 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

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.
Was This Post Helpful? 1
  • +
  • -

#7 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

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

This post has been edited by Plus: 05 February 2009 - 10:51 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1