pointers

in the most simple/easy to understand english, can someone explain to me what pointers are.
i got this code from a youtube tutorial and i have still yet to understand fully how it (pointers)works.

The most I know about pointers are simply that they can be use as a means to interlink data. for example, I interlinked the value of int firstpointer with *piPointer1 in the pointerfunction... Anyway, again, I don't full understand the way I interlinked the two; did I just interlink the with the oder I wrote them in?...

please help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using namespace std;

void pointerfunction(int*piPointer, int*piPointer2);

int main()
{
	int firstpointer;
	int secondpointer;
	int result;
	pointerfunction(&firstpointer, &secondpointer);
	
	cout << endl;
	result = (firstpointer + secondpointer);

	cout << result << endl; 

system("PAUSE");
return 0;
}

void pointerfunction(int*piPointer1, int*piPointer2)
{

	cout << "Enter the two numbers that you want to add: ";
	cin >> *piPointer1 >> *piPointer2;


}

http://cplusplus.com/doc/tutorial/pointers/

I personally found the above link to be helpful. To explain pointers, let me give you an example. Say you have a function that takes an array and performs some operations on it. Now, if your array is say 1000x1000, it's not gonna be fun/efficient to pass this into your function. Instead what we do is pass a pointer to the array. The pointer points to the array's location in the computer's memory. However, if you dereference the pointer, you can see what's actually being held in the computer's memory at the particular location.

I could explain some more, but I feel like I would just be repeating the material in the above link. Check it out and let us know if there's material you're still confused about.

Hope that helps.
zero one(thanks so much)

I now know that a pointer is a data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.

in my program &firstpointer points towards the address of my variable int firstpointer;, in the pointerfunction. And then in the void pointerfunction the Dereference operator (*plusthename) takes the address in my pointer (&firstpointer) and uses it to find the value my pointer was point to, this is the variable int firstpointer;
Just to clear things up a bit, &firstpointer is the address of the integer firstpointer. It can be held in a pointer like this:

int* ptr = &firstpointer;

Now ptr points to firstpointer, which means the value it stores is the address of firstpointer.
Last edited on
I think to make things clearer I would rename the variables below as.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main()
{
	// These are really value types not pointer types
	int firstValue; //firstpointer;
	int secondValue; //secondpointer;
	int result;
	// pointerfunction is declared to take 2 pointers to int's.
	// In your case when you call it you need to take the address of the 2 int's
	// you want to pass it.
	pointerfunction( &firstValue, &secondValue );
	
	cout << endl;
	result = (firstValue + secondValue);

system("PAUSE");
return 0;
}

void pointerfunction(int*piPointer1, int*piPointer2)
{
	// Here piPointer1 points to (contains address of) firstValue
	//      piPointer2 points to (contains address of) secondValue

	cout << "Enter the two numbers that you want to add: ";
	cin >> *piPointer1 >> *piPointer2;
}
closed account (D80DSL3A)
I used the following analogy when first learning about pointers to help me keep the concept grounded.

You are looking for Bob (he has some data you need) but you find Joe instead. What might you do? Ask Joe where Bob is!
haha, nice!
It's a lot easier to pass the address of a variable to a function instead of creating a new variable. Also, this way you can change the variable instead of just using its value until the function has finished executing. It's also a good alternative to global variables.
Topic archived. No new replies allowed.