学习自狄泰软件学院唐佐林老师C语言课程,文章中图片取自老师的PPT,仅用于个人笔记。
实验1
#include <stdio.h>
int main()
{
int i = 0;
int* pI;
char* pC;
float* pF;
pI = &i;
*pI = 10;
printf("%p, %p, %d\n", pI, &i, i);
printf("%d, %d, %p\n", sizeof(int*), sizeof(pI), &pI);
printf("%d, %d, %p\n", sizeof(char*), sizeof(pC), &pC);
printf("%d, %d, %p\n", sizeof(float*), sizeof(pF), &pF);
return 0;
}
mhr@ubuntu:~/work/C$
mhr@ubuntu:~/work/C$ ./a.out
0x7ffe8ce0710c, 0x7ffe8ce0710c, 10
8, 8, 0x7ffe8ce07110
8, 8, 0x7ffe8ce07118
8, 8, 0x7ffe8ce07120
mhr@ubuntu:~/work/C$
注意: 函数调用时候实参将复制给形参!!!
实验2
#include <stdio.h>
int swap(int* a, int* b)
{
int c = *a;
*a = *b;
*b = c;
}
int main()
{
int aa = 1;
int bb = 2;
printf("aa = %d, bb = %d\n", aa, bb);
swap(&aa, &bb);
printf("aa = %d, bb = %d\n", aa, bb);
return 0;
}
mhr@ubuntu:~/work/C$ gcc 26-2.c
mhr@ubuntu:~/work/C$
mhr@ubuntu:~/work/C$
mhr@ubuntu:~/work/C$ ./a.out
aa = 1, bb = 2
aa = 2, bb = 1
mhr@ubuntu:~/work/C$
实验3
#include <stdio.h>
int main()
{
int i = 0;
const int* p1 = &i;
int const* p2 = &i;
int* const p3 = &i;
const int* const p4 = &i;
*p1 = 1; // compile error
p1 = NULL; // ok
*p2 = 2; // compile error
p2 = NULL; // ok
*p3 = 3; // ok
p3 = NULL; // compile error
*p4 = 4; // compile error
p4 = NULL; // compile error
return 0;
}
来源:CSDN
作者:奶牛养殖场小马
链接:https://blog.csdn.net/LinuxArmbiggod/article/details/104050485