Pointers in C Programming- Coordinate conversion -
i should write program convert cartesian coordinates polar , vice versa use of pointers, wrote following code function gives me segmentation fault. tried without pointers , still doesn't send numbers function, can modify pointer code? i'm new c.
#include <stdio.h> #include <math.h> void cart(float *radius,float *degree) { float *x,*y,*radians; *radians= (3.14159265359/180) * *degree; *x= *radius * cos(*radians); *y= *radius * sin(*radians); } int main() { float radius, radians, degree; float x,y; int m; char c,p; printf(" enter c if converting cartesian polar \n"); printf(" enter p if converting polar cartesian \n"); scanf("%c",&m); if (m=='p') { printf("enter radius , angle separated comma \n"); scanf("%f,%f",&radius,°ree); cart(&radius,°ree); printf("cartesian form (%f,%f) \n",x,y); } else if (m=='c') { printf("enter values of x , y separated comma \n"); scanf("%f,%f",&x,&y); radius=sqrt(((x*x)+(y*y))); // finding radius radians=atan(y/x); //finding angle in radians printf("polar form (%f,%f) \n",radius,radians); //angle in radians } return 0; }
the first thing note in 'cart' function:
void cart(float *radius,float *degree) { float *x,*y,*radians; *radians= (3.14159265359/180) * *degree; *x= *radius * cos(*radians); *y= *radius * sin(*radians); }
you have declared pointers named x
, y
, radians
, not yet point anything.
so when 'de-reference' them *x
, *y
, *radians
accessing memory not exist, result in undefined behavior, possibly segmentation fault.
i assume goal x
, y
, radians
main function match those, should passing them function well.
Comments
Post a Comment