69,381
社区成员
发帖
与我相关
我的任务
分享
void setfoo(struct foo **fp)
{
(*fp)->i = 4;
(*fp)->str = "hehe";
}
#include <stdio.h>
#include <stdlib.h>
struct foo {
int i;
char *str;
};
void setfoo(struct foo *&fp)//这里最好你是传引用,或者你可以传指针的指针, 不然你就是传一个副本,没有用的
{
fp->i = 4;
fp->str = "hehe"; //这句话str你分配内存了吗
}
int main()
{
struct foo *fp ;
fp = (struct foo *)malloc(sizeof(struct foo));
setfoo(fp);
printf("fp->i = %d\n",fp->i);
return 0;
}
struct foo {
int i;
char *str;
};
void setfoo(struct foo *fp) //型参fp只是实参的副本,实参指针并没有获取到分配的内存,传引用或者2维指针
{
if((fp = (struct foo *)malloc(sizeof(struct foo))) != NULL)
{
fp->i = 4;
fp->str = "hehe";
}
}
int main()
{
struct foo *fp ;
setfoo(fp);
printf("fp->i = %d\n",fp->i);
return 0;
}