Pointers
Pointers are a new type of variable type. It deals with the specific location of where information is being stored in the computer as opposed to what is being stored in the variable. To make this point a little more understandable I have a video for you.
Introduction to Pointers
Deferencer
This is a symbol that is used in code to help us get a value store in a variable that we are accessing using a pointer.
So lets break that down.
When we have a pointer (meaning the location of where a piece of information in a variable is stored in our computer)
We can use a Deferencer (in code) to get the data stored in the variable using the pointer (location)
The deferencer is simply an *
So if a ran a line of code that said this;
cout << *ptr << endl;
The program will use the value stored in ptr to retrieve the information/data store in that memory location in the computer.
because of the cout, it will then output that data to the screen.
So lets break that down.
When we have a pointer (meaning the location of where a piece of information in a variable is stored in our computer)
We can use a Deferencer (in code) to get the data stored in the variable using the pointer (location)
The deferencer is simply an *
So if a ran a line of code that said this;
cout << *ptr << endl;
The program will use the value stored in ptr to retrieve the information/data store in that memory location in the computer.
because of the cout, it will then output that data to the screen.
Another new operator....
sizeof is a new operator that you have never used before.
This operator provides information on how many bytes of memory its argument occupies (or may occupy).
This operator provides information on how many bytes of memory its argument occupies (or may occupy).
The Assignment
Familiarize the student with: declaring pointer variables; assigning values to pointer variables; using incrementation to move the pointer through adjacent memory locations.
Look at the code below. It doesn't look scary, right? Your task doesn't look scary either – you just have to find the smallest element in the vector. But there's one condition – you mustn't use indexing. In other words, using brackets in your code is strictly prohibited. Hint: You may use as many pointers as you wish. Forgive us that we don't show you any sample output. We would much rather see your code – it's much more interesting.
Look at the code below. It doesn't look scary, right? Your task doesn't look scary either – you just have to find the smallest element in the vector. But there's one condition – you mustn't use indexing. In other words, using brackets in your code is strictly prohibited. Hint: You may use as many pointers as you wish. Forgive us that we don't show you any sample output. We would much rather see your code – it's much more interesting.
Sample Code
using namespace std;
int main(void)
{
int vector[ ] = { 3, -5, 7, 10, -4, 14, 5, 2, -13 };
int n = sizeof(vector) / sizeof(vector[0]);
// Insert your code here return 0;
}
int main(void)
{
int vector[ ] = { 3, -5, 7, 10, -4, 14, 5, 2, -13 };
int n = sizeof(vector) / sizeof(vector[0]);
// Insert your code here return 0;
}