I've been given this piece of code:
#include <stdio.h>
#include <math.h>
struct polar_coordinate{
double theta;
double r;
};
struct polar_coordinate * polar(double x, double y);
int main(void){
double x = 2.0;
double y = 3.0;
struct polar_coordinate * pci;
pci = polar(x,y);
printf("The coordinate x = %.1f and y = %.1f is
in polar coordinates theta = %.2f and r = %.2f\n ",x,y,pci->theta,pci->r);
}
struct polar_coordinate * polar(double x, double y){
struct polar_coordinate pc;
pc.r = sqrt(x*x + y*y);
pc.theta = atan2(y,x);
return &pc;
}
I am then told that the struct polar_coordinate * polar function has a bug, that I have to fix. I tried doing this by using:
struct polar_coordinate * polar(double x, double y){
struct polar_coordinate * pc;
pc->r = sqrt(x*x + y*y);
pc->theta = atan2(y,x);
return pc;
}
The code can then compile but if I try to run it I get a segmentation fault 11. But I can't really see what should be wrong.