*
) after int
means pointer toint * pIntExample1
int *pIntExample1
int* pIntExample1
- preferred
methodint intExample1 = 32;
int* pIntExample1 = &intExample1;
#include <iostream>
int intExample1 = 32;
int* pIntExample1 = &intExample1;
int main() {
std::cout << "intExample1 -> " << intExample1 << std::endl;
std::cout << "&pIntExample1 -> " << &pIntExample1 << std::endl;
return 0;
}
intExample1 -> 32
&pIntExample1 -> 0x5556c43fe018
0x1234
int
intExample1
is the name of the reserved
memory locationint intExample1 = 32;
int* pIntExample1 = &intExample1;
#include <iostream>
int intExample1 = 32;
int* pIntExample1 = &intExample1;
int main() {
std::cout << "intExample1 -> " << intExample1 << std::endl;
std::cout << "&pIntExample1 -> " << &pIntExample1 << std::endl;
return 0;
}
intExample1 -> 32
&pIntExample1 -> 0x5577552b5018
#include <iostream>
int arrayExample1[4] = {5, 0, 6, 2};
int main() {
for(int &element : arrayExample1) {
std::cout << "element -> " << element << " [Address: " << &element << "]" << std::endl;
}
return 0;
}
element -> 5 [Address: 0x55e34c89f010]
element -> 0 [Address: 0x55e34c89f014]
element -> 6 [Address: 0x55e34c89f018]
element -> 2 [Address: 0x55e34c89f01c]
nullptr
keywordnullptr
can be assigned to any pointer
type, but not to built-in data typesnullptr
and can be
used for every pointer typeint* pIntExample1 = nullptr;
double* pDoubleExample1 = nullptr;
int intExample1 = nullptr; // Throws an error as intExmaple1 is not a pointer
arrayExample1
containing four values
pointer1
is a pointer to the initial
elementpointer2
is a pointer to the initial
element (in a different syntax)pointer3
is a pointer to a different
element in the arraypointer4
is a pointer to a memory
address beyond the last elementint arrayExample1[4] = {5, 0, 6, 2};
int* pointer1 = arrayExample1;
int* pointer2 = &arrayExample1[0];
#include <iostream>
int arrayExample1[4] = {5, 0, 6, 2};
int main() {
int* pointer1 = arrayExample1;
std::cout << "pointer1 -> " << pointer1 << std::endl;
int* pointer2 = &arrayExample1[0];
std::cout << "pointer2 -> " << pointer2 << std::endl;
return 0;
}
pointer1 -> 0x55fce58ad010
pointer2 -> 0x55fce58ad010
int arrayExample1[4] = {5, 0, 6, 2};
int* pointer3 = arrayExample1+2;
int* pointer4 = arrayExample1+6;
#include <iostream>
int arrayExample1[4] = {5, 0, 6, 2};
int main() {
int* pointer3 = arrayExample1+2;
std::cout << "pointer3 -> " << pointer3 << std::endl;
int* pointer4 = arrayExample1+6;
std::cout << "pointer4 -> " << pointer4 << std::endl;
return 0;
}
pointer3 -> 0x560882c44018
pointer4 -> 0x560882c44028
#include <iostream>
char arrayExample1[] = {'5', '0', '6', '2'};
int main() {
for(char* pointer = arrayExample1; *pointer != 0; pointer++) {
std::cout << "*pointer -> " << *pointer << std::endl;
}
return 0;
}
*pointer -> 5
*pointer -> 0
*pointer -> 6
*pointer -> 2
#include <iostream>
char arrayExample1[] = {'5', '0', '6', '2'};
int main() {
for(char &element : arrayExample1) {
std::cout << "element -> " << element << std::endl;
}
return 0;
}
element -> 5
element -> 0
element -> 6
element -> 2
null
reference&
) characterint
declaration is a new
object being createdint
declaration (with the
&
) is the reference object
intExample1
#include <iostream>
int main() {
int intExample1 = 32;
int& rIntExample1 = intExample1;
std::cout << "intExample1 -> " << intExample1 << " [Address: " << &intExample1 << "]" << std::endl;
std::cout << "rIntExample1 -> " << rIntExample1 << " [Address: " << &rIntExample1 << "]" << std::endl;
return 0;
}
intExample1 -> 32 [Address: 0x7ffc9877cf8c]
rIntExample1 -> 32 [Address: 0x7ffc9877cf8c]
swap()
function consists of two
parameters
#include <iostream>
void swap(int &x, int &y) {
int tmpX;
tmpX = x;
x = y;
y = tmpX;
}
int main() {
int intExample1 = 5;
int intExample2 = 10;
std::cout << "[Before] intExample1 -> " << intExample1 << " [Address: " << &intExample1 << "]" << std::endl;
std::cout << "[Before] intExample2 -> " << intExample2 << " [Address: " << &intExample2 << "]" << std::endl << std::endl;
swap(intExample1, intExample2);
std::cout << "[After] intExample1 -> " << intExample1 << " [Address: " << &intExample1 << "]" << std::endl;
std::cout << "[After] intExample2 -> " << intExample2 << " [Address: " << &intExample2 << "]" << std::endl;
return 0;
}
[Before] intExample1 -> 5 [Address: 0x7ffd36ff8250]
[Before] intExample2 -> 10 [Address: 0x7ffd36ff8254]
[After] intExample1 -> 10 [Address: 0x7ffd36ff8250]
[After] intExample2 -> 5 [Address: 0x7ffd36ff8254]
#include <iostream>
int arrayExample1[4] = {5, 0, 6, 2};
int arrayLength = sizeof(arrayExample1) / sizeof(int);
int& set_value(int i) {
return arrayExample1[i];
}
int main() {
for(int i = 0; i < arrayLength; i++) {
std::cout << "[Before] arrayExample1[" << i << "] -> " << arrayExample1[i] << std::endl;
}
set_value(1) = -9;
for(int i = 0; i < arrayLength; i++) {
if(i == 0) {
std::cout << std::endl << "[After] arrayExample1[" << i << "] -> " << arrayExample1[i] << std::endl;
} else {
std::cout << "[After] arrayExample1[" << i << "] -> " << arrayExample1[i] << std::endl;
}
}
return 0;
}
[Before] arrayExample1[0] -> 5
[Before] arrayExample1[1] -> 0
[Before] arrayExample1[2] -> 6
[Before] arrayExample1[3] -> 2
[After] arrayExample1[0] -> 5
[After] arrayExample1[1] -> -9
[After] arrayExample1[2] -> 6
[After] arrayExample1[3] -> 2
new
and delete
keywords, respectivelynew
keywordint* pIntExample1 = new int;
*pIntExample1 = 32;
#include <iostream>
int main() {
int* pIntExample1 = new int;
*pIntExample1 = 32;
std::cout << "pIntExample1 -> " << *pIntExample1 << " [Address: " << pIntExample1 << "]" << std::endl;
return 0;
}
pIntExample1 -> 32 [Address: 0x55be05d432b0]
int
using the new
keywordnew
keyword returns the address of
the memory locationnew
keyword
returns the address of the first elementdelete
keyword#include <iostream>
int main() {
int* pIntExample1 = new int;
*pIntExample1 = 32;
std::cout << "pIntExample1 -> " << *pIntExample1 << " [Address: " << pIntExample1 << "]" << std::endl;
delete pIntExample1; // Deletes the variable and reserved memory
return 0;
}
pIntExample1 -> 32 [Address: 0x562ec2cc92b0]