1 int main() 2 { 3 int a[2] = { 1,2 }; 4 int* p = a; 5 cout << "a+1" << a + 1 << endl; 6 cout << "p+1" << p + 1 << endl; 7 cout << "*(a+1)" << *(a + 1) << endl; 8 cout << "*(p+1)" << *(p + 1)<< endl; 9 //a++;//数组名是右值,它不可以自增 10 p++; 11 system("pause"); 12 return 0; 13 }
注意不能通过形参指针求解实参数组的长度,因为指针在64位系统下长度永远是8个字节
1 #include<iostream> 2 using namespace std; 3 4 void fun(int *arr2) { 5 int temp = 0; 6 int length = sizeof(arr2) / sizeof(arr2[0]); 7 cout << length << endl; 8 for (int i = 0; i < length-1; i++) 9 { 10 for (int j = 0; j < length -1-i; j++) 11 { 12 if (arr2[j] > arr2[j+1]) 13 { 14 temp = arr2[j]; 15 arr2[j] = arr2[j + 1]; 16 arr2[j + 1] = temp; 17 } 18 } 19 } 20 } 21 int main() 22 { 23 int arr1[5] = { 8,5,2,4,1 }; 24 fun(arr1); 25 for (int i = 0; i < 5; i++) 26 { 27 cout << arr1[i] << endl; 28 } 29 system("pause"); 30 return 0; 31 }