二级指针与指针数组
#include <stdio.h>
#include <stdlib.h> int main() { // 定义一个指针数组,每个元素都是一个指向int的指针 int *ptr_array[3]; // 为指针数组的每个元素分配内存 ptr_array[0] = malloc(2*sizeof(int)); ptr_array[1] = malloc(2*sizeof(int)); ptr_array[2] = malloc(2*sizeof(int)); printf("%d\n", sizeof(int)); printf("%d\n", sizeof(int*)); printf("%d\n", sizeof(ptr_array)); // 定义一个二重指针,让它指向指针数组的第一个元素 int **double_ptr = &ptr_array[0]; //赋值方式(double_ptr[0][0]) = 40; (double_ptr[1][0]) = 50; (double_ptr[2][0]) = 60; (double_ptr[0][1]) = 70; (double_ptr[1][1]) = 80; (double_ptr[2][1]) = 90; // 通过二重指针访问和打印指针数组的元素 for (int i = 0; i < 3; i++) {//指针数组中每个元素占用8字节,代表指针数组元素的地址printf("double_ptr[%d] points to a value of %d\n", i, &(double_ptr[0])+i); printf("double_ptr[%d] points to a value of %d\n", i, (double_ptr+i)); printf("double_ptr[%d] points to a value of %d\n", i, (&double_ptr[i])); //代表每行第2元素中的地址printf("double_ptr[%d] points to a value of %d\n", i, *(&(double_ptr[0])+i)+1);printf("double_ptr[%d] points to a value of %d\n", i, *(double_ptr+i)+1); printf("double_ptr[%d] points to a value of %d\n", i, (double_ptr[i]+1)); printf("double_ptr[%d] points to a value of %d\n", i, &(double_ptr[i][0])+1); printf("double_ptr[%d] points to a value of %d\n", i, &(double_ptr[i][1])); //代表每行第二个元素值所指空间的内容printf("double_ptr[%d] points to a value of %d\n", i, *(*(&(double_ptr[0])+i)+1));printf("double_ptr[%d] points to a value of %d\n", i, *(*(double_ptr+i)+1));printf("double_ptr[%d] points to a value of %d\n", i, *(double_ptr[i]+1));printf("double_ptr[%d] points to a value of %d\n", i, *(&(double_ptr[i][0])+1)); printf("double_ptr[%d] points to a value of %d\n", i, (double_ptr[i][1])); } // 释放内存 for (int i = 0; i < 3; i++) { free(ptr_array[i]); } return 0;
}
二维数组、及通过二级指针访问二维数组
#include <stdio.h>
#include <stdlib.h> int main() { int a[3][2] = {{1,2},{3,4},{5,6}};//&a整个数组的地址,a数组首行地址,*a数组首行第一个元素地址,**a数组第一个元素的值printf("%d,%d,%d,%d\n",&a, a,*a,**a);//解释方式与上条语句类似printf("%d,%d,%d,%d\n",&a, &a[0],&a[0][0],a[0][0]);//第一种:二维数组指针,指向整个二维数组int (*ptr)[3][2]=&a;//第二种:一维数组指针,指向二维数组第一行元素int (*ptr1)[2]=a;//or &a[0]//第三种:int *型指针,指向二维数组第一行第一列的第一个元素int *ptr2=&a[0][0];//or *a//第四种:二级指针与二维数组结合int **ptr3 = calloc(3, sizeof(int *));for (int i = 0; i < 3; ++i) {*(ptr3+i)=*(a+i);printf("%d,%d\n",*(*(ptr3+i)+0),*(*(ptr3+i)+1));}int **ptr4 = calloc(3, sizeof(int *));for (int i = 0; i < 3; ++i) {printf("%d,%d\n",a+i,*(a+i));*(ptr4+i)=(a+i);printf("%d,%d\n",*(*(ptr4+i)+0),*(*(ptr4+i)+1));}return 0;
}
二维数组指针
#include <stdio.h>
#include <stdlib.h> int main(int argc, char **argv) {//二维数组指针int (*ptr)[3][3] = calloc(3 * 3, sizeof(int));//&ptr:二维数组指针的地址//ptr:二维数组的首地址,代表整个数组//*ptr:二维数组的首行首地址,代表整行//**ptr:二维数组的首个元素的地址,代表数组的第一个元素printf("%d,%d,%d,%d\n",&ptr,ptr,*ptr,**ptr);//赋值for (int i = 0; i < 3; ++i) {for (int j = 0; j < 3; ++j) {(*ptr)[i][j]=j;}}for (int i = 0; i < 3; ++i) {printf("%d,%d,%d\n",(*ptr)[i],(*ptr)+i,*((*ptr)+i));}for (int i = 0; i < 3; ++i) {printf("%d,%d\n",*((*ptr)[i]),**((*ptr)+i));}//打印for (int i = 0; i < 3; ++i) {for (int j = 0; j < 3; ++j) {printf("%d", (*ptr)[i][j]);}printf("\n");}return 0;
}