学习自狄泰软件学院唐佐林老师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$
来源:CSDN
作者:奶牛养殖场小马
链接:https://blog.csdn.net/LinuxArmbiggod/article/details/104059464