主题:[讨论]这样编码内存会有垃圾吗
[code=c]
#include"stdio.h"
#include"math.h"
typedef struct
{
double x; //直角坐标x
double y; //直角坐标y
} PointL;
typedef struct
{
double a; //弧度
double r; //极半径
} PointPolar;
PointJ getPointJ(PointL dot)
{
PointJ *p;
p = (PointJ*)malloc(sizeof(PointJ));
p -> a = atan2(dot.y, dot.x); //转化角度
p -> r = sqrt(s.x*s.x + s.y*s.y); //转化半径
return *p; //指针p 会消失吗? 内存中的数据会变垃圾吗?
}
int main()
{
PointL s;
s.x = 10;
s.y = 10;
PointJ p = getPointJ(s);
printf("%f\n", p.a);
}
[/code]
#include"stdio.h"
#include"math.h"
typedef struct
{
double x; //直角坐标x
double y; //直角坐标y
} PointL;
typedef struct
{
double a; //弧度
double r; //极半径
} PointPolar;
PointJ getPointJ(PointL dot)
{
PointJ *p;
p = (PointJ*)malloc(sizeof(PointJ));
p -> a = atan2(dot.y, dot.x); //转化角度
p -> r = sqrt(s.x*s.x + s.y*s.y); //转化半径
return *p; //指针p 会消失吗? 内存中的数据会变垃圾吗?
}
int main()
{
PointL s;
s.x = 10;
s.y = 10;
PointJ p = getPointJ(s);
printf("%f\n", p.a);
}
[/code]