指针变量,数组指针,指针数组,2级数组,函数指针,void 指针

前言

指针是c语言的重点,也是c语言的难点,一般最简单的指针变量我们不会弄错,可以遇到指针数组,数组指针,2维指针,3维指针,函数指针,void指针,有时候就蒙了,今天我花一天时间,学习整理一下,以供日后参考,防止学完后少用又忘记了。

指针变量

普通变量有地址和值。

指针变量也有地址和值,值存的是他址(普通变量地址)。

*name 访问的是他值(普通变量值)。

他型,指针变量声明的类型,是他值的类型(普通变量类型)。

指针变量+n,-n,是在他址的位置上下偏移(n * sizeof(他型))。

指针变量总结一句号:

值存他址,*访他值。

数组,2维数组,指针数组,数组指针,2维指针的区别

普通数组

#include 

int main()
{
    int arr[4] = {1,2,3,5};
    printf("arr[0] address = %x\n",&(arr[0]));
    printf("arr[1] address = %x\n",&(arr[1]));
    printf("arr[2] address = %x\n",&(arr[2]));

    printf("&(*(arr)) address = %x\n",&(*(arr)));
    printf("&(*(arr + 1)) address = %x\n",&(*(arr + 1)));
    printf("&(*(arr + 2)) address = %x\n",&(*(arr + 2)));

    printf("arr address = %x\n",arr);
    printf("arr + 1) address = %x\n",arr + 1);
    printf("arr + 2) address = %x\n",arr + 2);

    printf("*arr value = %d\n",*arr);
    printf("*(arr + 1) value = %d\n",*(arr + 1));
    printf("*(arr + 2) value = %d\n",*(arr + 2));

    printf("arr address = %x\n",&(arr));
    printf("arr address + 1 = %x\n",&(arr) + 1);
    printf("arr address + 2 = %x\n",&(arr) + 2);
    

    printf("arr address + 1 = %x\n",&arr + 1);
    printf("arr address + 2 = %x\n",&arr + 2);
    printf("arr address + 3 = %x\n",&arr + 3);
    return 0;
}

下标访问取地址

arr[0] address = c7365bb0
arr[1] address = c7365bb4
arr[2] address = c7365bb8

*号取地址
&(*(arr)) address = c7365bb0
&(*(arr + 1)) address = c7365bb4
&(*(arr + 2)) address = c7365bb8

arr值
arr address = c7365bb0
arr + 1) address = c7365bb4
arr + 2) address = c7365bb8

他值
*arr value = 1
*(arr + 1) value = 2
*(arr + 2) value = 3

自己地址
arr address = c7365bb0
arr address + 1 = c7365bc0
arr address + 2 = c7365bd0

总结:

arr其实就是一个常量指针,非常奇怪他值地址和自己地址相同,我把它称为指向自己的指针。

定义完后,arr不能重新赋值。

arr指向数组首地址。(地址类型为int)

&arr自己地址。(地址类型为int[4])+ 1会偏移 sizeof(int) * 4

二维数组

    int arrs[2][3] = {{1,2,3},{3,4,5}};

    printf("arrs[0] address = %x\n",&(arrs[0]));
    printf("arrs[0][0] address = %x\n",&(arrs[0][0]));
    printf("arrs[0][1] address = %x\n",&(arrs[0][1]));
    printf("arrs[1] address = %x\n",&(arrs[1]));

    printf("&(*(arrs) address = %x\n",&(*(arrs)));
    printf("&(*(arrs + 1) address = %x\n",&(*(arrs + 1)));

    printf("arrs address = %x\n",arrs);
    printf("arrs + 1 address = %x\n",arrs + 1);
    
    printf("&arrs address = %x\n",&arrs);
    printf("&arrs + 1 address = %x\n",&arrs + 1);

    printf("(*arrs) + 1 address = %x\n",(*arrs) + 1);

    printf("arr[1][2] value = %d\n", *(*(arrs + 1)) + 2);

arrs[0] address = a892b070
arrs[0][0] address = a892b070
arrs[0][1] address = a892b074
arrs[1] address = a892b07c
&(*(arrs) address = a892b070
&(*(arrs + 1) address = a892b07c
arrs address = a892b070
arrs + 1 address = a892b07c
&arrs address = a892b070
&arrs + 1 address = a892b088
(*arrs) + 1 address = a892b074
arr[1][2] value = 5
 

二维数组和一维数据的区别:己址,己值,他值,操作维度上升。

&arrs己址,值类型为(int[2][3]),偏移量为 sizeof(int) * 2 * 3

arrs己值,值类型为(int[3])arrs + 1 偏移量为 sizeof(int) * 3

*arrs他值,值为(int[3]数组首地址),值类型为(int)arr + 1为sizeof(int)

*(*arrs),值为int

数组指针

    int (*p)[3];
    int a[2][3] = {{1,2,3},{4,5,6}};

    p = a;
    printf("&p address = %x\n",&p);
    printf("p address = %x\n",p);
    printf("p+1 address = %x\n",p + 1);
    printf("a[1][1] address = %x\n",(*(p + 1) + 1));
    printf("a[1][1] = %d\n",*(*(p + 1) + 1));

&p address = af57d528
p address = af57d510
p+1 address = af57d51c
a[1][1] address = af57d520
a[1][1] = 5
 

这里定义的p它是一个指向数组长度为3的指针变量。相当于行指针。

己址(&p):就是指针变量的地址,值类型为(int*),偏移量为sizeof(int *)

己值(p):数组长度为3的首地址。值类型为(int[3]),偏移量为sizeof(int)* 3

他值(*p):2维数组首地址的值(也就是a[0])。值类型为(int) ,偏移量为sizeof(int)

a[0]的值为1维数组首地址。

*((*p + i) + j):他值的他值a[i][j]位置的值

指针数组

int *p[3];
    int a[2][3] = {{1,2,3},{4,5,6}};
    int i;
    for (i = 0; i < 3; i++)
    {
        printf("&(p + %d) address = %x\n",i,&(p) + i);
        printf("&(p[%d]) address = %x\n",i,&(p[i]));
        printf("&(*(p + %d)) address = %x\n",i,&(*(p + i)));
    }
    p[0] = &a[1][1];
    p[1] = a[0];
    printf("*p[0] value = %d\n",*(p[0]));
    printf("*p[0] value = %d\n",*(*(p)));

    printf("a[0][1] value = %d\n",*(p[1] + 1));
    printf("a[0][1] value = %d\n",*(*(p + 1) + 1));

&(p + 0) address = bf0e30e0
&(p[0]) address = bf0e30e0
&(*(p + 0)) address = bf0e30e0
&(p + 1) address = bf0e30f8
&(p[1]) address = bf0e30e8
&(*(p + 1)) address = bf0e30e8
&(p + 2) address = bf0e3110
&(p[2]) address = bf0e30f0
&(*(p + 2)) address = bf0e30f0
*p[0] value = 5
*p[0] value = 5
a[0][1] value = 2
a[0][1] value = 2
 

p实际上是一个长度为3类型为指针的数组。

己址(&p):p数组的首地址,值类型为(int*)[3],偏移量为sizeof(int *) * 3

己值(p):首地址p[0]的值,值类型为(int *),偏移量为sizeof(int *)

他值(*p):相当于*p[0],值类型为(int),偏移量为sizeof(int)

*((*p + i) + j):他值的他值a[i][j]位置的值

数组指针和指针数组的区别:

数组指针是一个指针,指针数组是多个指针。

它们在己址,己值值类型上面刚好相反。

二级指针

    int **p = NULL;
    int *p1 = NULL;
    int a[2][3] = {{1,2,3},{4,5,6}};
    p1 = a[0];
    p = &p1;

    printf("p1 address = %x\n",&p1);
    printf("p1 + 1 address = %x\n",&p1 + 1);
    printf("p1 value = %x\n",p1);
    printf("p address = %x\n",&p);
    printf("p + 1 address = %x\n",&p + 1);
    printf("p value = %x\n",p);

    printf("p1 a[0][1] value = %d\n",*(p1 + 1));
    printf("p a[0][1] value = %d\n",*(*(p) + 1));
    return 0;

p1 address = b000e120
p1 + 1 address = b000e128
p1 value = b000e100
p address = b000e128
p + 1 address = b000e130
p value = b000e120
p1 a[0][1] value = 2
p a[0][1] value = 2

p是一个指针一级指针的指针

己址(&p):值类型为(int**),偏移量为sizeof(int**)

己值(p):值类型为(int*),偏移量为sizeof(int*)

他值(*p):值类型为(int*),偏移量为sizeof(int*)

*((*p + i) + j):他值的他值a[i][j]位置的值

函数指针

    int (*f)(int);
    f = fun;
    printf("f address = %x\n",&f);
    printf("fun address = %x\n",&fun);
    printf("*f address = %x\n",*f);
    f(1);

f address = f49988e8
fun address = 40052d
*f address = 40052d
fun 1
p是一个指向返回值为int,参数1为int的函数入口地址。

void指针

不清楚类型的指针,任何指针都可以赋值给void指针。

常用于泛型编程,作为公共接口函数的参数。

你可能感兴趣的:(c语言,c语言,指针)