###### tags: `程設筆記`
{%hackmd theme-dark %}
# Chaper 7 Pointers
## What is Pointers?
* Declaration: int *pointer_name;/char *pointer_name;
* Data being stored inside a pointer is the address(unsigned int: hex) of a data in the memory.
* Pointer shoud be initialized. A pointer with null or the value 0 is called null pointer.(0 is the only integer that can be assigned to a pointer variable)
ex:

## How to assign the address of a data to pointers?
* "*" and "&" are inverses of one another.
ex:
```c++=
#include <iostream>
#include <string>
using namespace std;
int main(){
int *pointer;
int a=10;
pointer=&a;
cout<<pointer<<"\n"; //cout the address of a.
cout<<*pointer<<"\n";cout<<the value of a.
cout<<*&a; //cout the value of a.
}
```
## The relations between pointers and data
* alter a <=> alter the value of *pointer.
* The address won't be changed.
ex:
```c++=
#include <iostream>
#include <string>
using namespace std;
int main(){
int *pointer;
int a=10;
pointer=&a;
cout<<pointer<<" "<<*pointer<<" "<<a<<"\n";
*pointer=100;
cout<<pointer<<" "<<*pointer<<" "<<a<<" \n";
}
```
## PassByReference with Pointers
ex:
```c++=
#include <iostream>
using namespace std;
void cube(int *pointer){
*pointer= *pointer * *pointer * *pointer;
}
int main(){
int length;
while(cin>>length){
cube(&length);
cout<<length<<"\n";
}
}
```
## Const and Pointers
```c++=
const int* pointer1=# //The number pointer1 points to is a const.
int* const pointer2=# //pointer is a const.
```

* For pointer1:

* For pointer2:

ex(For pointer1):
```c++=
#include <iostream>
using namespace std;
int main(){
int a=10;
const int* pointer;
pointer=&a;
cout<<*pointer<<"\n"; //cout 10
a=100;
cout<<*pointer<<"\n"; //cout 100
/*pointer=100;*/ //not allowed
}
```
ex(For pointer2):
```c++=
#include <iostream>
using namespace std;
int main(){
int a=10, b=100;
int* const pointer=&a;
*pointer=1000;
cout<<a<<"\n";
/*pointer=&b*/ //not allowed
}
```
## Pointers and Array
* int array and pointers
```c++=
#include <iostream>
using namespace std;
int main(){
int a[5]={0, 1, 2, 3, 4};
int *pointer=a; //a is the address of a[0]
for(int i=0; i<5; i++){
cout<<*(a+i)<<" "; //cout 0~4
}
cout<<endl;
for(int i=0; i<5; i++){
*(a+i)=0;
}
for(int i=0; i<5; i++){
cout<<*(a+i)<<" "; //cout 0
}
cout<<endl;
cout<<&a[4]-pointer; //4 the distance(elements) between a[4] and pointer(a[0]). = 2*sizeof(int)
}
```
* char array and pointers
```c++=
#include <iostream>
using namespace std;
int main(){
char a[18]="Helloworld";
cout<<a<<"\n"; //it won't cout the address of a[0]
cout<<a+5<<"\n"; //it will cout world
cout<<*(a+9)<<"\n"; //equals to a[10];
cout<<(void*)a<<"\n"; //it will cout the address of a[0](H)
cout<<(void*)a+9<<"\n"; //it will cout the address of a[9](d)
}
```
* c-string and pointers

* sizeof()
* sizeof() with array or value will return the total size in bytes.
Ex1(with array):
```c++=
#include <iostream>
using namespace std;
int main(){
int arr[5]={0, 1, 2, 3, 4};
cout<<sizeof(arr)<<"\n"; //cout the total size of the array(bytes)
cout<<sizeof(arr)/sizeof(arr[0]); //cout the total amount of elements in arr.
}
```
Ex2(with value):
```c++=
#include <iostream>
using namespace std;
int main(){
double a=10.5;
int b=10;
cout<<sizeof(a+b); //return 8 since the result of int and double addition is still double
}
```
Ex3(with c-string):
```c++=
#include <iostream>
using namespace std;
int main(){
char string[11]="Helloworld";
cout<<sizeof(string);
}
```
* void* => a pointer for any genre of data type.
* Before using it, the type of pointer must be determined.
```c++=
#include <iostream>
using namespace std;
int main(){
int a=10;
void* pointer;
pointer=&a;
cout<<pointer<<"\n"; //cout the address
cout<<*(int*)pointer<<"\n"; //cout the number a correctly
}
```
* Use pointers to print a 2d array
```c++=
#include <iostream>
using namespace std;
void print_array(int *a, int rows, int columns){
for(int i=0; i<rows; i++){
a=a+i*columns;
for(int j=0; j<columns; j++){
cout<<*(a+j)<<" ";
}
cout<<"\n";
}
}
int main(){
int a[2][2]={{0, 1}, {2, 3}};
print_array(&a[0][0], 2, 2);
}
```
* Pointers for 2D array

* Pointers for 3D array

* Arrays of Pointers
* The array stores the address of the first character in each c-string.
Ex1:
```c++=
const char * const suit[ 4 ] =
{ "Hearts", "Diamonds",
"Clubs", "Spades" };
```

Ex2:
```c++=
#include <iostream>
using namespace std;
int main(){
char *suit[2]={"Hello", "World"}; //stores the address of the first character in each string
for(int i=0; i<sizeof(suit)/8; i++){
cout<<suit[i]<<" ";
}
}
```
## Function's Pointer
* Pass the pointer of a function to another fucntion
```c++=
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
void swap(int &a, int &b){
int temp=a;
a=b;
b=temp;
}
void selection_sort(vector <int> &v, void (*swap)(int &a, int &b)){
for(int i=0; i<v.size()-1; i++){
for(int j=i+1; j<v.size(); j++){
if(v[j]<v[i]){
swap(v[j], v[i]);
}
}
}
for(int i=0; i<v.size(); i++){
cout<<v[i]<<" ";
}
}
int main(){
vector <int> test={1, 2, 5, 3, 7, 6, 10};
selection_sort(test, swap);
}
```
## Selection sort
Reference(How it works):https://www.geeksforgeeks.org/selection-sort/
* Easy to program but inefficient.
Ex:
```c++=
#include <iostream>
#include <vector>
using namespace std;
void swap_double(int &a, int &b){
double temp=a;
a=b;
b=temp;
}
void sort(vector<int> &vec){
for(int i=0; i<vec.size()-1; i++){
for(int j=i+1; j<vec.size(); j++){
if(vec[j]<vec[i]){
swap_double(vec[j], vec[i]);
}
}
}
}
int main(){
vector<int> test={0, 7, 3, 5, 10};
sort(test);
for(int i=0; i<test.size(); i++){
cout<<test[i]<<" ";
}
}
```