C基础第28课--指针和数组分析(上)

南楼画角 提交于 2020-01-22 00:51:34

学习自狄泰软件学院唐佐林老师C语言课程,文章中图片取自老师的PPT,仅用于个人笔记。


在这里插入图片描述

实验1

#include <stdio.h>

int main()
{
    int a[5] = {0};
    int* p = NULL;
    
    printf("a = 0x%X\n", (unsigned int)(a));
    printf("a + 1 = 0x%X\n", (unsigned int)(a + 1));
    
    printf("p = 0x%X\n", (unsigned int)(p));
    printf("p + 1 = 0x%X\n", (unsigned int)(p + 1));
    
    return 0;
}


mhr@ubuntu:~/work/C$ 
mhr@ubuntu:~/work/C$ ./a.out 
a = 0xCD11B2D0
a + 1 = 0xCD11B2D4
p = 0x0
p + 1 = 0x4
mhr@ubuntu:~/work/C$ 
mhr@ubuntu:~/work/C$ 

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

实验2

#include <stdio.h>

int main()
{
    char s1[] = {'H', 'e', 'l', 'l', 'o'};
    int i = 0;
    char s2[] = {'W', 'o', 'r', 'l', 'd'};
    char* p0 = s1;
    char* p1 = &s1[3];
    char* p2 = s2;
    int* p = &i;
	
    printf("%d\n", p0 - p1);
   // printf("%d\n", p0 + p2);
   // printf("%d\n", p0 - p2);
    //printf("%d\n", p0 - p);
    //printf("%d\n", p0 * p2);
    //printf("%d\n", p0 / p2);
	
    return 0;
}


mhr@ubuntu:~/work/C$ 
mhr@ubuntu:~/work/C$ gcc 28-2.c
28-2.c: In function ‘main’:
28-2.c:13:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
     printf("%d\n", p0 - p1);
            ^
mhr@ubuntu:~/work/C$ 
mhr@ubuntu:~/work/C$ 
mhr@ubuntu:~/work/C$ 
mhr@ubuntu:~/work/C$ 
mhr@ubuntu:~/work/C$ ./a.out 
-3
mhr@ubuntu:~/work/C$ 

在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!