字符、字符串、字符数组、字符串数组、结构体数组的指针访问

字符、字符串、字符数组、字符串数组、结构体数组的指针访问

测试一

#include <stdio.h>

void test0()
{
    
    
    printf("//==========test0_start===========\n");
    char c;
    char *pc;
    /*第一步:所有变量都保存在内存中,我们打印一下变量的存储地址*/
    printf("&c =%p\n", &c);
    printf("&pc =%p\n", &pc);
    /*第二部:所有的变量都可以保存某些值,接着赋值并打印*/
    c = 'A';
    pc = &c;
    printf("c  =%c\n", c);
    printf("pc =%p\n", pc);
    /*第三部步:使用指针》1)取值   2)移动指针*/
    printf("*pc=%c\n", *pc);
    printf("//==========test0_end===========\n");
}

void test1()
{
    
    
    printf("//==========test1_start===========\n");
    int ia;
    int *pi;
    char *pc;
    /*第一步:所有变量都保存在内存中,我们打印一下变量的存储地址*/
    printf("&ia =%p\n", &ia);
    printf("&pi =%p\n", &pi);
    printf("&pc =%p\n", &pc);
    printf("//=====================\n");
    /*第二部:所有的变量都可以保存某些值,接着赋值并打印*/
    ia = 0x12345678;
    pi = &ia;
    pc = (char *)&ia;
    printf("ia =0x%x\n", ia);
    printf("pi =%p\n", pi);
    printf("pc =%p\n", pc);
    printf("//=====================\n");
    /*第三部步:使用指针》1)取值   2)移动指针*/
    printf("*pi =0x%x\n", *pi);

    printf("pc =%p\t", pc);
    printf("*pc =0x%x\n", *pc);
    pc++;
    printf("pc =%p\t", pc);
    printf("*pc =0x%x\n", *pc);
    pc++;
    printf("pc =%p\t", pc);
    printf("*pc =0x%x\n", *pc);
    pc++;
    printf("pc =%p\t", pc);
    printf("*pc =0x%x\n", *pc);

    printf("//==========test1_end===========\n");
}
void test2()
{
    
    
    printf("//==========test2_start===========\n");
    char arr[3] = {
    
    'A', 'B', 'C'};
    char *pc;
    /*第一步:所有变量都保存在内存中,我们打印一下变量的存储地址*/
    printf("arr =%p\n", arr);
    printf("&arr=%p\n", &arr);
    printf("&pc =%p\n", &pc);
    /*第二部:所有的变量都可以保存某些值,接着赋值并打印*/
    pc = arr;
    printf("pc =%p\n", pc);
    /*第三部步:使用指针   1)取值   2)移动指针*/
    printf("pc =%p\t", pc);
    printf("*pc =0x%x\n", *pc);
    pc++;
    printf("pc =%p\t", pc);
    printf("*pc =0x%x\n", *pc);
    pc++;
    printf("pc =%p\t", pc);
    printf("*pc =0x%x\n", *pc);

    printf("//==========test2_end===========\n");
}

void test3()
{
    
    
    printf("//==========test3_start===========\n");
    char *str = "hello linux!";
    printf("all_str =%s\n", str);
    while (*str != '\0')
    {
    
    
        printf("str =%p\t", str);
        printf("*str =%c\n", *str);
        str++;
    }
    printf("//==========test3_end===========\n");
}
void test4()
{
    
    
    printf("//==========test4_start===========\n");
    char *str[] = {
    
    "hello", "come", "on"};
    char **pstr;
    /*第一步:所有变量都保存在内存中,我们打印一下变量的存储地址*/
    printf("str =%p\n", str);
    printf("&str=%p\n", &str);
    printf("&pstr =%p\n", &pstr);
    /*第二部:所有的变量都可以保存某些值,接着赋值并打印*/
    pstr = str;
    printf("pstr =%p\n", pstr);
    /*第三部步:使用指针   1)取值   2)移动指针*/

    /*指针等价于数组*/
    printf("pstr =%p\t", pstr);
    printf("**pstr =%s\n", pstr[0]);

    printf("pstr =%p\t", pstr);
    printf("**pstr =%s\n", pstr[1]);

    printf("pstr =%p\t", pstr);
    printf("**pstr =%s\n", pstr[2]);
    printf("\n\n\n");
    /*移动指针*/
    printf("pstr =%p\t", pstr);
    printf("**pstr =%s\n", *pstr);
    pstr++;
    printf("pstr =%p\t", pstr);
    printf("**pstr =%s\n", *pstr);
    pstr++;
    printf("pstr =%p\t", pstr);
    printf("**pstr =%s\n", *pstr);

    printf("//==========test4_end===========\n");
}

int main(int argc, char const *argv[])
{
    
    
    printf("//===========main==========\n");
    printf("sizeof (char   )= %d\n", sizeof(char));
    printf("sizeof (int    )= %d\n", sizeof(int));
    printf("sizeof (char  *)= %d\n", sizeof(char *));
    printf("sizeof (char **)= %d\n", sizeof(char **));
    printf("//===========main==========\n");
    printf("\n");
    test0();
    printf("\n");
    test1();
    printf("\n");
    test2();
    printf("\n");
    test3();
    printf("\n");
    test4();
    return 0;
}

运行结果

//===========main==========
sizeof (char   )= 1
sizeof (int    )= 4
sizeof (char  *)= 4
sizeof (char **)= 4
//===========main==========

//==========test0_start===========
&c =0xff98abff
&pc =0xff98abf8
c  =A
pc =0xff98abff
*pc=A
//==========test0_end===========

//==========test1_start===========
&ia =0xff98abfc
&pi =0xff98abf8
&pc =0xff98abf4
//=====================
ia =0x12345678
pi =0xff98abfc
pc =0xff98abfc
//=====================
*pi =0x12345678
pc =0xff98abfc	*pc =0x78
pc =0xff98abfd	*pc =0x56
pc =0xff98abfe	*pc =0x34
pc =0xff98abff	*pc =0x12
//==========test1_end===========

//==========test2_start===========
arr =0xff98abfd
&arr=0xff98abfd
&pc =0xff98abf8
pc =0xff98abfd
pc =0xff98abfd	*pc =0x41
pc =0xff98abfe	*pc =0x42
pc =0xff98abff	*pc =0x43
//==========test2_end===========

//==========test3_start===========
all_str =hello linux!
str =0x565f1f13	*str =h
str =0x565f1f14	*str =e
str =0x565f1f15	*str =l
str =0x565f1f16	*str =l
str =0x565f1f17	*str =o
str =0x565f1f18	*str = 
str =0x565f1f19	*str =l
str =0x565f1f1a	*str =i
str =0x565f1f1b	*str =n
str =0x565f1f1c	*str =u
str =0x565f1f1d	*str =x
str =0x565f1f1e	*str =!
//==========test3_end===========

//==========test4_start===========
str =0xff98abf4
&str=0xff98abf4
&pstr =0xff98abf0
pstr =0xff98abf4
pstr =0xff98abf4	**pstr =hello
pstr =0xff98abf4	**pstr =come
pstr =0xff98abf4	**pstr =on



pstr =0xff98abf4	**pstr =hello
pstr =0xff98abf8	**pstr =come
pstr =0xff98abfc	**pstr =on
//==========test4_end===========

测试二


#include <stdio.h>

typedef struct R
{
    
    
    int a;
    int b;
    char *name;
} Test;

Test s[3] = {
    
    {
    
    1, 2, "Sam"}, {
    
    3, 4, "Mark"}, {
    
    5, 6, "Json"}};

void comparison()
{
    
    

    int arr[10] = {
    
    0};

    printf("arr = %p\n", arr); //arr 为数组首元素的地址     指向其内部元素的指针

    printf("&arr= %p\n", &arr); //&arr 为整个数组的地址      指向整个数组的指针

    printf("arr+1 = %p\n", arr + 1);

    printf("&arr+1= %p\n", &arr + 1);

    /*
        arr = 0060FED8
        &arr= 0060FED8

        arr+1 = 0060FEDC    跳过了数组的元素的首地址,指向了数组下一个元素的地址
        &arr+1= 0060FF00    跳过了整个数组的地址
      */
}

int main(void)
{
    
    

    comparison();
    /*

比如 int Arr[10] ,那么这个数组的类型其实就是 int [ 10 ],
读者这里要注意。
那么大家知道,对指针进行++操作,就是给它加上其类型的大小。
https://blog.csdn.net/YeLing0119/article/details/83541727
*/

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

    Test(*array)[3]; //array这个指针变量是Test[3]*型的,占四个字节
    array = &s;      //指向s
    Test const *p = s;

    printf("sizeof(Test)=%d\n", sizeof(Test));
    printf("sizeof(s)=%d\n", sizeof(s));
    printf("sizeof(p)=%d\n", sizeof(p));
    printf("sizeof(array)=%d\n", sizeof(array));

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

    printf("p = %p\n", p); //每次加Test*,12个字节
    printf("p+1 = %p\n", p + 1);
    printf("p+2 = %p\n", p + 2);

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

    /*
    *用方括号取值,如果方括号左边是个数组,
    *那么数组会被隐式转换为首元素指针右值,然后对这个值进行的解引用。
    */
    printf("s[0] = %s\n", s[0].name);
    printf("s[1] = %d\n", s[1]);
    printf("s[2] = %d\n", s[2]);

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

    printf("array = %p\n", array);       //array这个指针变量是Test[3]*型的
    printf("array+1 = %p\n", array + 1); //跳过了整个s数组的地址
    printf("array+2 = %p\n", array + 2);

    printf("\n用数组访问隐式转换后等价于指针\n");

    printf("array[0] = %p\n", array[0]);
    printf("array[1] = %p\n", array[1]); //指针使用数组中的值,跳过了整个数组的地址
    printf("array[2] = %p\n", array[2]);

    printf("the array[0]->name is %s\n", array[0]->name);
    printf("the array[1]->name is %s\n", array[1]->name); //null,因为跳过了整个数组的地址

    return 0;
}

运行结果

arr = 0xffd73ef8
&arr= 0xffd73ef8
arr+1 = 0xffd73efc
&arr+1= 0xffd73f20




sizeof(Test)=12
sizeof(s)=36
sizeof(p)=4
sizeof(array)=4



p = 0x565ed040
p+1 = 0x565ed04c
p+2 = 0x565ed058



s[0] = Sam
s[1] = 3
s[2] = 5



array = 0x565ed040
array+1 = 0x565ed064
array+2 = 0x565ed088

用数组访问隐式转换后等价于指针
array[0] = 0x565ed040
array[1] = 0x565ed064
array[2] = 0x565ed088
the array[0]->name is Sam
the array[1]->name is (null)

猜你喜欢

转载自blog.csdn.net/qq_28816873/article/details/104433453
今日推荐