该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
在函数中对结构体赋值时赋不上,用printf查看结果始终为0,有人说是因为结构体指针使用前没有进行实例化导致。一知半解没明白什么叫实例化,好像是要用malloc分配内存。请问各位高人:
(1)实例化是什么操作?
什么东西使用前必须实例化?是结构体使用前必须实例化?还是指针使用前必须实例化?还是什么别的……?
(2)我试过单独对结构体指针进行赋值,代码如下,为什么不需要所谓的实例化操作?所有赋值都正常(用printf语句查看),代码如下:
#include
struct STU{
double X;
double Y;
double Z;
};
struct STU *pStu;
void function1(struct STU *fp)
{
fp->X = 8987.9987;
fp->Y = 1235.5555;
fp->Z = 6542.0225;
printf("\n In function1 fp->X = %lf", fp->X);
printf("\n In function1 fp->Y = %lf", fp->Y);
printf("\n In function1 fp->Z = %lf", fp->Z);
printf("\n");
}
void function2()
{
function1(pStu);
printf("\n In function2 pStu->X = %lf", pStu->X);
printf("\n In function2 pStu->Y = %lf", pStu->Y);
printf("\n In function2 pStu->Z = %lf", pStu->Z);
printf("\n");
}
int main(void) {
function2();
printf("\n In main pStu->X = %lf", pStu->X);
printf("\n In main pStu->Y = %lf", pStu->Y);
printf("\n In main pStu->Z = %lf", pStu->Z);
printf("\n");
return 0;
}
运行结果:
In function1 fp->X = 8987.998700
In function1 fp->Y = 1235.555500
In function1 fp->Z = 6542.022500
In function2 pStu->X = 8987.998700
In function2 pStu->Y = 1235.555500
In function2 pStu->Z = 6542.022500
In main pStu->X = 8987.998700
In main pStu->Y = 1235.555500
In main pStu->Z = 6542.022500
运行结果赋值正常,没有用到什么实例化操作啊
请高手解答,谢谢!