xxxxxxxxxx
type *ptr; // Declare a pointer variable called ptr as a pointer of type
// or
type* ptr;
// or
type * ptr; // I shall adopt this convention
A pointer is a variable that points to a location in memory. Here is an example below:
xxxxxxxxxx
#include <iostream>//For those unaware, the lib "iostream" stands for input output stream and is used mostly for stuff like cout or cin.
//int main() is the main function which will run upon running the exe file.
int main() {
int x = 10;//The variable we are looking to point too
int* xptr = &x;//using "&" you get the momory location of the variable x and you are telling xptr to point to x's location.
std::cout<<xptr<<std::endl;//prints to location it's pointing too
std::cout<<&x<<std::endl;//If you did it correctly it will output the same thing as xptr
std::cout<<*xptr<<std::endl;//It will print the value(10) of the variable its pointing too. this is called derefrencing
std::cout<<x;//It will print 10.
return 0;
}
xxxxxxxxxx
int myvar = 6;
int pointer = &myvar; // adress of myvar
int value = *pointer; // the value the pointer points to: 6
xxxxxxxxxx
Pointers are datatypes that point to a value of another
variable using its memory adress. Pointers are declared as following:
int* pointer;
The value of pointers is the adress of the variable. When someone
wishes to adress the value behind the adress, he can do it like this:
*pointer = newValue;
The *-operator (dereference operator) can also be read
as "value pointed to by". The example above could in that way be
read as "Value pointed to by pointer is now newValue".
Note that the asterisk (*) used when declaring a pointer only
means that it is a pointer (it is part of its type compound pecifier),
and should not be confused with the dereference operator.
You can assign the adress of variables to pointers:
int foo1 = 24;
int* pointer = &foo1;
The &-operator (adress-of-operator) can be read as "adress-of".
Therefore the line with our pointer declaration can be read like
"int-pointer with name pointer has now the value of the adress of foo1"
xxxxxxxxxx
#include <iostream>
using namespace std;
int main()
{
int x=5;
int *ptr=&x;
cout<<&x<<endl; //prints the address of the variable (x)
cout<<ptr<<endl; //prints the address of the variable (x)
cout<<*ptr<<endl; //prints the value of x(5)
cout<<&ptr<<endl; //prints the address of the pointer (ptr)
}
xxxxxxxxxx
#include <stdio.h>
int f1(int x) { return x * 2; }
int f2(int x) { return x * 4; }
int main()
{
int (*fptr1)(int) = &f1;
fptr1 = &f2;
fptr1(3);
}
xxxxxxxxxx
//why do we use pointers:
1)pass values by refrence to a function
2)return multiple values from a function
3)use pointers in combinational with arrays
4)dynamic memory allocation
5)use pointers in a base class in order to access object of derived class (Smart pointers)
xxxxxxxxxx
/*
Here is how pointers work in a nustshell(represent | as what's
happening in the memory and / as a place in the ram):
int x = 2; | 2/0x00ef5
int *z = &x| 0x00ef5/0x00ef6 (See how the value of the pointer z
matches with the memory address of x? that's how they work!)
when you print out the pointer as *n (replace n with the var name)
it will actually look at the value
see it's a memory address
go to that memory address
and print the value originally in the memory address which is 2
Here is code:
*/
int x = 5;
int *y = &x;
cout << *y+1;
/*
the reason why i did *y+1 was so to show that after it got
the value from the memory address it will add 1
*/