指针作为函数参数的情形
2008-02-27 14:22
225 查看
1. 形参在函数结束后释放。所以对形参的赋值不起作用。而实参为空
#include <iostream.h>
void func(int** ptr)
{
int* p = new int(66);
cout<<ptr<<endl; // ptr is 0x00000000
ptr = &p;
cout<<ptr<<endl; // ptr is 0x0012ff1c
}
int main()
{
int** np = NULL;
func(np);
cout<<np<<endl; // ptr is 0x00000000
return 0;
}
2.形参指向的指针分配了内存,函数结束后形参释放。形参指向的指针没有变化
void func(int** ptr)
{
int* p = new int(66);
cout<<ptr<<endl;
*ptr = p;
cout<<ptr<<endl;
}
int main()
{
int* p = NULL;
int** np = &p;
func(np);
cout<<*p<<endl;
cout<<**np<<endl;
return 0;
}
输出 66,66
注意分别形参本身和形参所指向的对象之间的差别
#include <iostream.h>
void func(int** ptr)
{
int* p = new int(66);
cout<<ptr<<endl; // ptr is 0x00000000
ptr = &p;
cout<<ptr<<endl; // ptr is 0x0012ff1c
}
int main()
{
int** np = NULL;
func(np);
cout<<np<<endl; // ptr is 0x00000000
return 0;
}
2.形参指向的指针分配了内存,函数结束后形参释放。形参指向的指针没有变化
void func(int** ptr)
{
int* p = new int(66);
cout<<ptr<<endl;
*ptr = p;
cout<<ptr<<endl;
}
int main()
{
int* p = NULL;
int** np = &p;
func(np);
cout<<*p<<endl;
cout<<**np<<endl;
return 0;
}
输出 66,66
注意分别形参本身和形参所指向的对象之间的差别
相关文章推荐
- 函数指针作为某个函数的参数及定义函数指针
- 结构体变量、结构指针变量、结构数组作为函数的参数应用实例分析 .
- 函数传出参数,用指针作为函数的传出参数实现多值返回
- 指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为参数,泛型函数
- C语言-----内存与指针(3) How 4000 do you pass a string to a function如何给一个函数传入字符串作为参数:
- 指针作为函数参数
- 结构体变量、结构指针变量、结构数组作为函数的参数应用实例分析
- 数组作为函数参数传递时退化为指针(腾讯)
- c++之指针作为函数参数传递的问题
- C++指针作为函数的参数进行传递时注意的问题
- c_指针_一维数组名作为函数参数
- 以结构指针作为函数参数
- 函数指针也可作为函数的参数
- 项目二.2(指针类型作为成员函数的参数)
- 结构体变量指针作为函数参数
- 结构体指针作为函数参数
- 如何理解数组作为函数参数时,数组名就是指针?
- 成员函数指针作为参数传递给其他函数和普通函数指针的传递
- C++基础之指针作为函数的参数
- c++ 成员函数作为函数指针参数传入