Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,127 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,034 people online right now. Registration is fast and FREE... Join Now!




Pointer Help

 
Reply to this topicStart new topic

Pointer Help, Don't understand pointers....

NelsonTT
post 6 Oct, 2008 - 04:48 PM
Post #1


New D.I.C Head

*
Joined: 5 Oct, 2008
Posts: 16


My Contributions


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:
  1. How are pointers useful?
  2. What does the * operator do?
  3. How does the following program work?

CODE

#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! cool.gif
User is offlineProfile CardPM

Go to the top of the page

NelsonTT
post 6 Oct, 2008 - 07:16 PM
Post #2


New D.I.C Head

*
Joined: 5 Oct, 2008
Posts: 16


My Contributions


Anyone going to answer this?
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 7 Oct, 2008 - 04:36 AM
Post #3


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 501



Thanked 44 times

Dream Kudos: 25
My Contributions


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.
cpp
#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;
}
User is online!Profile CardPM

Go to the top of the page

KYA
post 7 Oct, 2008 - 05:46 AM
Post #4


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,176



Thanked 48 times

Dream Kudos: 1150
My Contributions


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
User is online!Profile CardPM

Go to the top of the page

NelsonTT
post 7 Oct, 2008 - 08:37 AM
Post #5


New D.I.C Head

*
Joined: 5 Oct, 2008
Posts: 16


My Contributions


Thanks everyone!! I sorta understand pointers now...
User is offlineProfile CardPM

Go to the top of the page

Bench
post 7 Oct, 2008 - 01:53 PM
Post #6


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 600



Thanked 8 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


Have a look at this article about pointers on Eternallyconfuzzled
http://www.eternallyconfuzzled.com/tuts/la...t_pointers.aspx


And another one here
http://www.daweidesigns.com/cgi-bin/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: 7 Oct, 2008 - 01:59 PM
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 11:39AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month