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?...
usingnamespace 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;
}
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.
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;
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;
}
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.