tags:

views:

186

answers:

6

alright, im looking at a code here and the idea is difficult to understand.

#include <iostream>
using namespace std;
class Point
{
public :
    int X,Y;
    Point() : X(0), Y(0) {}
};

void MoveUp (Point * p)
{
    p -> Y += 5;
}

int main()
{
    Point point;
    MoveUp(&point);
    cout << point.X << point.Y;
    return 0;
}

Alright, so i believe that a class is created and X and Y are declared and they are put inside a constructor

a method is created and the argument is Point * p, which means that we are going to stick the constructor's pointer inside the function;

now we create an object called point then call our method and put the pointers address inside it?

isnt the pointers address just a memory number like 0x255255?

and why wasnt p ever declared?

(int * p = Y)

what is a memory addres exactly? that it can be used as an argument?

+3  A: 

A pointer is simply a variable like any other but it holds a memory address.
Read that line about 6 times. The name pointer itself seems scary to people, but it is really just called this to make it easier for ourselves to think about what is happening.

The type of a pointer (for example char*, int*, Point*) simply tells the compiler what is stored at that memory address.

So all pointers are alike in that they all store a simple memory address. But they are different in that the type of the pointer will tell you what will be contained at that memory address.

and why wasnt p ever declared?

p is declared:

//p is declared to be a variable that holds an address
// and at that address it holds a type Point
//p itself only holds an address not the actual data of the Point.
void MoveUp (Point * p)
{
  //The -> operator when applied to a pointer, means give me the object at
  // that address and then access the following member
  p->Y += 5;
}

what is a memory addres exactly? that it can be used as an argument?

You can think of a memory address simply as a number. On 32-bit programs this number is 4 bytes long. On 64-bit programs this number is 8 bytes long.


Consider the following code:

Point point;
Point *p = &point;

The point variable is of type Point. It holds an object of the class Point.
The &point expression returns an address of the Point variable point. The expression &point is of type Point*.
The p variable holds a address of type Point, the type of p is Point*. p holds the address in memory of the object point.

Brian R. Bondy
+1  A: 

p is declared in the function definition

void MoveUp (Point * p)
Xavier Combelle
+9  A: 

p was declared.

void MoveUp (Point * p)
{
    p -> Y += 5;
}

is a function that will take a pointer to a Point and add 5 to its Y value. It's no different to the following:

void f(int n) {
    printf ("%d\n", n);
}
:
int x = 7;
f(x);

You wouldn't say n wasn't defined in that case. It's the same for p in your case.

Perhaps some comments in the code would help:

#include <iostream>
using namespace std;
class Point
{

public :
    int X,Y;
    Point() : X(0), Y(0) {}       // Constructor sets X and Y to 0.
};

void MoveUp (Point * p)           // Take a point pointer p.
{
    p -> Y += 5;                  // Add 5 to its Y value.
}
int main()
{
    Point point;                  // Define a point.
    MoveUp(&point);               // Call MoveUp with it's address.
    cout <<point.X << point.Y;    // Print out its values (0,5).
    return 0;
}

Pointers are simply a level of indirection. In the code:

1   int X;
2   int *pX = &X;
3   X = 7;
4   *pX = 7;

the effect of lines 3 and 4 are identical. That's because pX is a pointer to X so that *pX, the contents of pX, is actually X.

In your case, p->Y is the same as (*p).Y, or the Y member of the class pointed to by p.

paxdiablo
TimothyTech
paxdiablo
DeadMG
<nitpick>Functions that aren't members of objects aren't called methods</nitpick>... ignore me... long day
Cogwheel - Matthew Orlando
@Cogwheel, thanks, I've changed it. I always appreciate things like that being pointed out since it (usually) ends up making the answers better.
paxdiablo
+1  A: 

Before you look at pointers, you have to clarify for yourself the meaning of "class", "instance" and "constructor". Your sentence

a class is created and X and Y are declared and they are put inside a constructor

shows the need for such clarification. Here is a discussion on books you may want to read: http://stackoverflow.com/questions/1048000/books-to-refer-for-learning-oop-through-c

Arkadiy
+1  A: 

Think about memory as contiguous small blocks.

Now, think about the Point class as something that is 2 blocks width, and you put it in any place of the memory. It doesn't matter where, the important thing is that it is placed in only ONE starting point. You can move it and change that staring point, but irrevocably will start in ONE starting point.

Then the pointer is the number of that ONE starting point.

When you pass a pointer as an argument, you doing

  • you: 'hey function, take this Point and do what you know!'
  • function: 'ok, but where is the Point!?'
  • you: 'oh, sorry, i don't take it with me, it's located there, in that block!'

And the function will know now that THERE is a Point, and with the magic of the compiler, the function will know that its 2 blocks width. Then the function takes it, change its Y (thanks again, compiler), and leave it where it was before (in fact, it didn't even take it away from there). Minutes later, you go there, and see Point.Y has changed. You say 'thanks', and you go.

Diego Pereyra
A: 

p was declared as a method parameter.

Pointers are a special kind of variable that hold the memory address of a value. The * symbol is the dereference symbol, which tells your code to go lookup the value IN the memory address held by the pointer. Now is also a good time to introduce the & symbol, which tells your code to get the memory address of a value. For example:

int i = 5;    //int
int *pointer; //int pointer

pointer = &i; //sets pointer to the memory address of i

doMath(&i); //passes a memory address, value inside that address is being used
doMath(pointer); //same as above
dontMath(i); //value of x will be 207, value of i is still 7 since a copy is being modified

//value of pointer is a memory address
//value of *pointer is the value stored inside that memory address, 7
//value of &pointer is the memory address of pointer, which itself holds the memory address


void doMath(int *p) {
   *p++; 
}
void dontMath(int x){
   x=x+200;
}

The confusion I had early on was the meaning of the * symbol. In variable declaration, it means that your are declaring a pointer for a certain datatype (a variable that holds a memory address). Elsewhere, it is the dereference symbol telling your code to resolve the value.

baultista