C语言学习--指针和数组、const修饰指针

数组名:【数组名是地址常量】-- 不可以被修改

#include<stdio.h>

int main (void)
{
    int a[3] = {1,2,3};//a是数组名, 数组名是地址常量,不能被修改

    int b[3] ;

    printf("the a is %x", a);

    b = a ;// 这样的code 是错误的, 因为b代表的含义是数组int b[]的地址, 但是数组名是常量, 是不能被修改的.



}

指针是变量, 可以使用数组名给指针复制

#include<stdio.h>

int main (void)
{
    int a[3] = {1,2,3};//a是数组名, 数组名是地址常量,不能被修改


    int b[3] ;

    int *p = b;//p是指针变量, b是地址常量, 可以把常量复制给变量


}

取数组元素

 

 

#include<stdio.h>

int main (void)
{
    int a[3] = {1,2,3};//a是数组名, 数组名是地址常量,不能被修改

    for (int i=0 ; i < 3; i++)
    {
        printf("the i values is %d\n", a[i]);
    }

}
#include<stdio.h>

int main (void)
{
    int a[3] = {1,2,3};//a是数组名, 数组名是地址常量,不能被修改

    for (int i=0 ; i < 3; i++)
    {
        printf("the i values is %d\n", a[i]);  // a[i] == *(a+i)
    }


    printf("-----------------------\n");
    for (int i=0 ; i < 3; i++)
    {
        printf("the i values is %d\n", *(a+i));// a[i] == *(a+i)
    }


    printf("-----------------------\n");
    int *p = a;  // a是地址常量, 复制给p
    for (int i=0 ; i < 3; i++)
    {
        printf("the i values is %d\n", p[i]); // p就是a
    }


     printf("-----------------------\n");
    //int *p = a;
    for (int i=0 ; i < 3; i++)
    {
        printf("the i values is %d\n", *(p+i));
    }

}

the i values is 1
the i values is 2
the i values is 3
-----------------------
the i values is 1
the i values is 2
the i values is 3
-----------------------
the i values is 1
the i values is 2
the i values is 3
-----------------------
the i values is 1
the i values is 2
the i values is 3




 指针和数组的区别

指针是变量,数组是常量

sizeof(指针) ---> 4字节

sizeof(数组) --》 数组的实际字节数

指针++  操作数组

 

 

 

 

 

 

 

#include<stdio.h>

int main (void)
{
    //使用指针++操作数组, p++


    int arr[] = {0,1,2,3,4,5,6,7,8,9};

    int* p = arr; //arr是元素的气势地址位

    int n = sizeof(arr)/ sizeof(arr[0]);


    for (int i = 0; i < n; i++)
    {
        printf("%d\n", *p);  //每次循环之后间接引用的地址位都是偏移到下个元素的开始位置
        p++; //p++ 每次会对p增加四字节的地址位
    }

}


0
1
2
3
4
5
6
7
8
9
#include<stdio.h>

int main (void)
{
    //使用指针++操作数组, p++


    int arr[] = {0,1,2,3,4,5,6,7,8,9};

    int* p = arr; //arr是元素的气势地址位

    int n = sizeof(arr)/ sizeof(arr[0]);

    for (int i = 0; i < n; i++)
    {
        printf("p is %p\n",p);
        printf("%d\n", *p);  //每次循环之后间接引用的地址位都是偏移到下个元素的开始位置
        p++; //p++ 每次会对p增加四字节的地址位
    }


}


p is 000000000061FDE0
0
p is 000000000061FDE4  // 每次增加四字节
1
p is 000000000061FDE8
2
p is 000000000061FDEC
3
p is 000000000061FDF0
4
p is 000000000061FDF4
5
p is 000000000061FDF8
6
p is 000000000061FDFC
7
p is 000000000061FE00
8
p is 000000000061FE04
9

 

 

指针类型的作用

(1) 间接引用 :  决定从指针存储的地址开始, 向后读取的字节数 (与指针本身的存储空间无关)

(2)加减运算

#include<stdio.h>

int main (void)
{
    int a = 10;

    char  b = 'a';

    short c = 11;

    printf("sizeof int %d\n",sizeof(a));
    printf("sizeof char %d\n",sizeof(b));
    printf("sizeof short %d\n",sizeof(c));

    printf("--------------------------------\n");

    int d = 0x12345648; //d所占的空间是四字节

    int* p1 = &d;

    char* p2 = &d;

    short* p3 = &d;

    printf("*p1 is %p\n", *p1);//只引用4字节, 只读d内空间的四个字节, 相当于全读
    printf("*p2 is %p\n", *p2);//只引用1字节
   printf("*p3 is %p\n", *p3); }//只引用2字节



sizeof int 4
sizeof char 1
sizeof short 2
--------------------------------
*p1 is 0000000012345648
*p2 is 0000000000000048
*p3 is 0000000000005648

 

 

 

 数据类型在加减运算中的作用

#include<stdio.h>

int main (void)
{
    //不同的指针类型, 决定了指针进行+1 操作向后加过的字节数
    printf("--------------------int  int  int  int int------------------\n");
    int a = 10;

    int* p = &a; //p变量里面的名字就是a的地址

    printf("the p is %d\n", p);
    printf("the &a is %d\n", &a);
    printf("the p+1 is %d\n", p+1);


    printf("--------------------short short short----------------------\n");
    short* p1 = &a; //p变量里面的名字就是a的地址

    printf("the p1 is %d\n", p1);
    printf("the &a is %d\n", &a);
    printf("the p1+1 is %d\n", p1+1);

    printf("--------------------char char char-------------------------\n");


    char* p2 = &a; //p变量里面的名字就是a的地址

    printf("the p2 is %d\n", p2);
    printf("the &a is %d\n", &a);
    printf("the p2+1 is %d\n", p2+1);


}


--------------------int  int  int  int int------------------
the p is 6422020
the &a is 6422020
the p+1 is 6422024
--------------------short short short----------------------
the p1 is 6422020
the &a is 6422020
the p1+1 is 6422022
--------------------char char char-------------------------
the p2 is 6422020
the &a is 6422020
the p2+1 is 6422021

 

posted @ 2022-10-24 21:51  朵朵奇fa  阅读(54)  评论(0编辑  收藏  举报