一级指针 #include<stdio.h> #include<windows.h>
void print(int *p,int sz) { int i = 0; for (;i < sz;i++) { printf("%d “, *(p + i)); } printf(”\n"); } int main() { int a[10] = { 1,2,3,4,5,6,7,8,9,0 }; int *p = a; int sz = sizeof(a) / sizeof(a[0]); print(a, sz); system(“pause”); return 0; } 二级指针 #include<stdio.h> #include<windows.h>
void print(int **p) { printf("%d\n",**p); } int main() { int n = 10; int *t = &n; int **p = &t; print§; system(“pause”); return 0; } 指针和数组 #include<stdio.h> #include<windows.h>
void print_arr1(int a[3][5], int row,int col) { for (int i = 0;i < row;i++) { for (int j = 0;j < col;j++) { if(a[i][j]) printf("%d “, a[i][j]); } } printf(”\n"); } void print_arr2(int (*a)[5], int row, int col) { for (int i = 0;i < row;i++) { for (int j = 0;j < col;j++) { if (a[i][j]) printf("%d “, a[i][j]); } } printf(”\n"); } int main() { int arr[3][5] = {1,2,3,4,5,6,7,8,9,10}; print_arr1(arr,3,5); print_arr2(arr,3,5); system(“pause”); return 0; } 指针数组
数组中的元素是指针。