//
// Created by zing on 6/1/2020.
//
#include
using namespace std;
void define() {
//1ãæéçå®ä¹
int a = 10; //å®ä¹æ´ååéa
//æéå®ä¹è¯æ³ï¼ æ°æ®ç±»å * åéå ;
int *p;
//æéåéèµå¼
p = &a; //æéæååéaçå°å
cout << &a << endl; //æå°æ°æ®açå°å
cout << p << endl; //æå°æéåép
//2ãæéç使ç¨
//éè¿*æä½æéåéæåçå
å
cout << "*p = " << *p << endl;
}
void space() {
int a = 10;
int *p;
p = &a; //æéæåæ°æ®açå°å
cout << *p << endl; //* è§£å¼ç¨
cout << sizeof(p) << endl;
cout << sizeof(char *) << endl;
cout << sizeof(float *) << endl;
cout << sizeof(double *) << endl;
}
void null_pointer() {
//æéåépæåå
åå°åç¼å·ä¸º0ç空é´
int *p = NULL;
//访é®ç©ºæéæ¥é
//å
åç¼å·0 ~255为系ç»å ç¨å
åï¼ä¸å
è®¸ç¨æ·è®¿é®
cout << *p << endl;
}
void wild_pointer() {
//æéåépæåå
åå°åç¼å·ä¸º0x1100ç空é´
int *p = (int *) 0x1100;
//访é®éæéæ¥é
cout << *p << endl;
}
void const_pointer() {
int a = 10;
int b = 10;
//constä¿®é¥°çæ¯æéï¼æéæåå¯ä»¥æ¹ï¼æéæåçå¼ä¸å¯ä»¥æ´æ¹
const int *p1 = &a;
int const *pp2 = &a;
p1 = &b; //æ£ç¡®
pp2 = &b; //æ£ç¡®
a = 20;
//*p1 = 100; //æ¥é
//*pp2 = 1;
//constä¿®é¥°çæ¯å¸¸éï¼æéæåä¸å¯ä»¥æ¹ï¼æéæåçå¼å¯ä»¥æ´æ¹
int *const p2 = &a;
//p2 = &b; //é误
*p2 = 100; //æ£ç¡®
cout << *p2 << endl;
//constæ¢ä¿®é¥°æéå修饰常é
const int *const p3 = &a;
//p3 = &b; //é误
//*p3 = 100; //é误
}
void array() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p = arr; //æåæ°ç»çæé
cout << "第ä¸ä¸ªå
ç´ ï¼ " << arr[0] << endl;
cout << "æé访é®ç¬¬ä¸ä¸ªå
ç´ ï¼ " << *p << endl;
for (int i = 0; i < 10; i++) {
//å©ç¨æééåæ°ç»
cout << *p << endl;
p++;
}
}
//å¼ä¼ é
void swap1(int a, int b) {
int temp = a;
a = b;
b = temp;
}
//å°åä¼ é
void swap2(int *p1, int *p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void function() {
int a = 10;
int b = 20;
swap1(a, b); // å¼ä¼ éä¸ä¼æ¹åå®å
swap2(&a, &b); //å°åä¼ é伿¹åå®å
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
//åæ³¡æåºå½æ°
void bubbleSort(int *arr, int len) //int * arr ä¹å¯ä»¥å为int arr[]
{
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; 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;
}
}
void sort() {
int arr[10] = {4, 3, 6, 9, 1, 2, 10, 8, 7, 5};
int len = sizeof(arr) / sizeof(int);
bubbleSort(arr, len);
printArray(arr, len);
}
int main(int argc, char *argv[]) {
define();
space();
//null_pointer();
//wild_pointer();
const_pointer();
array();
function();
sort();
return 0;
}