指针做形参
2016-11-16 15:28
190 查看
指针做形参
下面两个函数分别改变指针指向的内容和指针值,探索*p = i与p = &i的不同void point(int *p) { printf("2. p = %d, addr p = %d, the p is %p\n", *p, &p, p);//其地址变了,说明是另一变量;指向的内存块数据和值没变 int i = 10; *p = i; printf("3. p = %d, addr p = %d, the p is %p\n", *p, &p, p);//地址和值没变;指向的内存块数据改变 i = 7; printf("4. p = %d, addr p = %d, the p is %p\n", *p, &p, p);//地址和值指向的内存块数据都不变 } void point1(int *p) { printf("7. p = %d, addr p = %d, the p is %p\n", *p, &p, p);//其地址变了,说明是另一变量;指向的内存块数据和值没变 int i = 10; p = &i; printf("8. p = %d, addr p = %d, the p is %p\n", *p, &p, p);//p的值改变为i的地址,即p指向i,此时p与r分别指向不同的内存块了,不会互相影响 i = 7; printf("9. p = %d, addr p = %d, the p is %p\n", *p, &p, p);//p指向i,i指向的内存数据改变,p也跟着改变 } int main() { int i = 6; int a = 6; int *q = &i; int *r = &a; printf("1. q = %d, addr q = %d, the q is %p\n", *q, &q, q); point(q); printf("5. q = %d, addr q = %d, the q is %p\n", *q, &q, q); printf("6. r = %d, addr r = %d, the r is %p\n", *r, &r, r); point1(r); printf("10. r = %d, addr r = %d, the r is %p\n", *r, &r, r); return 1; }
print 1. q = 6, addr q = 3340996, the q is 0032FAD4 2. p = 6, addr p = 3341004, the p is 0032FAD4 3. p = 10, addr p = 3341004, the p is 0032FAD4 4. p = 10, addr p = 3341004, the p is 0032FAD4 5. q = 10, addr q = 3340996, the q is 0032FAD4 6. r = 6, addr r = 3341000, the r is 0032FAD0 7. p = 6, addr p = 3341004, the p is 0032FAD0 8. p = 10, addr p = 3341004, the p is 0032FAD8 9. p = 7, addr p = 3341004, the p is 0032FAD8 10. r = 6, addr r = 3341000, the r is 0032FAD0 请按任意键继续. . .
传入的指针仅仅是一个拷贝,方法不会改变原指针的地址、值,但是可能会改变原指针所指向内存块的数据。
相关文章推荐
- 17周 项目2.3 求字符串的长度 并返回 用指针做形参
- 第17周项目2--去除str中的特定字符c(空格),结果保存到原字符串中(用指针做形参)
- C语言程序设计 练习题参考答案 第七章 (2) 指针与数组 main函数形参
- 三级指针作形参(命令行参数处理)
- 【C++总结】函数形参,返回const对象与非const对象,成员函数,函数指针
- Cpp--改变形参指针无法实现双向传值的问题
- 第十五周oj平台(3):指针当形参
- 数组形参与指针形参
- 第十六周 项目二:用指针玩字符串(2.2.2:去除字符串str的特定字符(用指针作形参))
- 第十六周 项目二:用指针玩字符串(2.6.1:去除句子中多余的空格(用指针作形参))
- 数组作为函数形参和用指针作为函数形参
- 第16周项目2-去除句子中第一个单词前的空格(指针作形参)
- 杂货边角(13):利用可变参数模板根据函数指针得到返回值类型和形参类型
- C语言----指针形参(指向指针的指针形参)
- 函数中的形参问题(指针形参、引用形参、二重指针作为形参)
- 引用形参 指针形参
- C++,常量,const,constant,引用,指针,形参,实参,函数,返回值
- 函数的形参(非引用形参、指针形参、引用形参)
- 17周项目二,1,(指针做形参)
- 17周项目二,5,(指针做形参)