C语言的指针

来源:互联网 发布:anaconda ubuntu 路径 编辑:程序博客网 时间:2024/05/14 04:12

华电北风吹
日期:2017-05-25

指针,数组,二级指针,指针数组,数组指针

#include <stdio.h>#include <stdlib.h>int main(){    int val;    int arr1[3] = { 1, 2, 3 };    int arr2[2][3] = { 1, 2, 3, 4, 5, 6 };    int *p;    int *p1[3];  //指针数组    int(*p2)[3]; //数组指针    int ** p3;   //二级指针    int i;    /* point type*/    p = &val;    p = arr1;  //数组名是一个地址,但不等同于指针    p1[0] = p;    p2 = &arr1; //对于数组&操作是取整个数组    p2 = arr2;  // 多维数组名可以看作降一维后类型的数组指针    p3 = &p;   // 二级指针,指向指针的指针    p3 = p1;   // 数组名也是一个地址,指针数组的地址当然也算是二级指针了    //error : p3 = &arr1;  //参考上面对数组取&操作解释    /*point size*/    printf("%d\n", sizeof(arr2));    p = (int *)(&arr1 + 1); p--;  // 快速找数组最后一个元素    printf("%d\n", *p);    /*malloc*/    p = (int *)malloc(3 * sizeof(int));    for (i = 0; i < 3; i++) {        p[i] = i + 1;    }    p3 = (int **)malloc(3 * sizeof(int *)); // 二级指针使用的时候每一个元素分配的都是一个指针    for (i = 0; i < 3; i++) {        p3[i] = p;    }    p3[0][0] = 2;    printf("%d\n", p3[1][0]);    return 0;}
原创粉丝点击