指针变量如何实现传址调用?
通常一个变量可以做到传值或传址调用,对于指针变量如何实现传址调用?请高手赐教,最好给出一个简单的例子。
[解决办法]
啥具体要求
&操作不就行了么
[解决办法]
int **p, *i;
*i = 256;
func(int **P)
{
**p = 333;
}
[解决办法]
int i = 0, j = 1;
int *p = &i;
int fun(int **p)
{
*p = &j;
return **p;
}
void __fastcall TForm1::Button5Click(TObject *Sender)
{
int *ptr = &i;
ShowMessage(IntToStr(fun(&ptr))); //显示1, 而不是0
}
[解决办法]
int *p = NULL;
void Fun(int **p)
{
*p = new int[100];
}
//使用:
Fun(&p);
p[0] = 1;
[解决办法]
void NewForm(TForm* &p) //指针引用
{
p=new TForm(Application);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TForm *pForm;
NewForm(pForm);
pForm-> ShowModal();
delete pForm;
}
[解决办法]
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello ");
printf(str);
}