int a = 10;
int * p;
p = &a; // & is to get the address. As 16 not decimal.
int a = 10;
int * p;
p = &a;
*p = 1000;
cout<< "a = "<< a << endl; // cout 1000
cout<< "*p = "<< *p << endl; // cout 1000
int a = 10;
int * p = &a;
cout << "sizeof (int *) = " << sizeof(p) << endl; // output is 8
int *p = Null; // Void pointer
*p = 100; // Output an error as 0~255 storage code is for system usage, can't be used for pointer.
int *x = (int *)0x1100; // Wild pointer: refers to a storage directly.
int a = 10;
int b = 10;
const int *p = &a; // pointer to const
*p = 20; // ERROR
p = &b; // reachable
int a = 10;
int b = 10;
int * const p = &a; // const pointer
*p = 20; // reachable
p = &b; // ERROR
int a = 10;
int b = 10;
const int * const p = &a; // const pointer to const
*p = 20; // ERROR
p = &b; // ERROR
int arr[5] = {1,2,3,4,5};
int *p = arr; // arr is the 1st address of array
cout << *p << endl; // output 1
p++; // add one int byte (8 in 64 system/ 4 in 32 sysem) to pointer
cout << *p << endl; // output 2; since arr is also int type, p pointer now refers to the 2nd address
int *p2 = arr;
for (int i = 0; i<5; i++){
cout<< *p2 << endl;
p2++;
}
void swap(int *p1, int *p2){
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main(){
int a = 10;
int b = 20;
swap(&a, &b); // value pass isn't OK while address is OK;
// Different address of parameter and argument though they share the same value;
// So, address pass is OK.
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
# include <iostream>
using namespace std;
void bubblesort(int *arr, int len){ // arr is a pointer, *arr is to get the value
for (int i = 0; i<len; i++){
for (int j = 0; j<len-i-1; j++){
if (arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void printarray(int *arr, int len){
for (int i = 0; i < len; i++){
cout<< arr[i] << endl;
}
}
int main(){
int arr[5] = {13,18,5,46,21};
int len = sizeof(arr) / sizeof(arr[0]);
bubblesort(arr, len); // arr is the variable name, also the pointer refers to the address.
printarray(arr, len);
return 0;
}