Pointer Help

Don't understand pointers....

Page 1 of 1

5 Replies - 735 Views - Last Post: 07 October 2008 - 02:53 PM Rate Topic: -----

#1 NelsonTT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 05-October 08

Pointer Help

Post icon  Posted 06 October 2008 - 05:48 PM

Hi, it's me again and I need help understanding pointers...

From what I understand, pointers are objects that hold memory addresses.
& returns a memory address. I don't understand what the * operator does.

My questions are:
  • How are pointers useful?
  • What does the * operator do?
  • How does the following program work?

#include <iostream>
using namespace std;

int main()
{
	 int total, val;
	 int *ptr;

	 total = 3200;
	 ptr = &total;
	 val = *ptr;
	 cout << "Total is: " << val << '\n';

	 return 0;
}



Thanks in advance! B)

Is This A Good Question/Topic? 0
  • +

Replies To: Pointer Help

#2 NelsonTT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 05-October 08

Re: Pointer Help

Posted 06 October 2008 - 08:16 PM

Anyone going to answer this?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Pointer Help

Posted 07 October 2008 - 05:36 AM

The * operator is the dereferencing operator. It goes to the memory address at the location and returns the contents of that memory location as the type associated with the pointer. See my comments in the code below.
#include <iostream>
using namespace std;

int main()
{
     // Let's say total gets a memory address of 0x1000, 
     // val gets a memory address of 0x2000
     int total, val;

     // And ptr gets a memory address of 0x3000
     int *ptr;

     // The memory at location 0x1000 is set to 3200.
     total = 3200;

     // The address of total, 0x1000, is written to the memory 
     // location at 0x3000
     ptr = &total;

     // The memory at location 0x3000 is read and found to 
     // point to memory location 0x1000. The value at memory 
     // location 0x1000 is copied to the memory location 0x2000.
     val = *ptr;

     cout << "Total is: " << val << '\n';

     return 0;
}

Was This Post Helpful? 1
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Pointer Help

Posted 07 October 2008 - 06:46 AM

Pointers are useful because they allow you yo access variables by reference. When a normal variable is passed in to a function as a parameter, a copy of it is made and used for all functions within the method. This is fine if you're simply returning the end result of that, but often one will want to directly modify the value passed in. So we create a pointer as a parameter and pass in a reference of the variable. Pointers are powerful, elegant, and quite often a pain in the ass if used incorrectly
Was This Post Helpful? 1
  • +
  • -

#5 NelsonTT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 05-October 08

Re: Pointer Help

Posted 07 October 2008 - 09:37 AM

Thanks everyone!! I sorta understand pointers now...
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Pointer Help

Posted 07 October 2008 - 02:53 PM

Have a look at this article about pointers on Eternallyconfuzzled
http://www.eternally...t_pointers.aspx


And another one here
http://www.daweidesi...in/pointers.php



Edit: Here's an FAQ on pointers from the C-FAQ website (everything contained here is for C, but nearly all of it also applies to C++ )
http://www.c-faq.com/ptrs/index.html

This post has been edited by Bench: 07 October 2008 - 02:59 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1