Reputation: 131
The error is say : 'mypoint' is not declare in this scope.
my struct is
struct point
{
int x;
int y;
};
and my code is :
struct point lineangle(int x1, int y1, int x2, int y2, int n){
double angle=360/n, s,c;
int rotated_x,rotated_y;
DDA(x1,y1,x2,y2);
for(int i=0;i<n;i++){
c = cos(angle*3.14/180);
s = sin(angle*3.14/180);
rotated_x= (x1 +((x2-x1)*c-(y2-y1)*s));
rotated_y= (y1 +((x2-x1)*s+(y2-y1)*c));
struct point mypoint[]={};
mypoint[i].x=x1;
mypoint[i].y=y1;
mypoint[i+1].x = rotated_x;
mypoint[i+1].y = rotated_y;
// DDA(x1,y1,rotated_x,rotated_y);
x2=rotated_x;
y2=rotated_y;
}
return mypoint;
}
i was declare but it not detected.
Upvotes: 0
Views: 133
Reputation: 2598
1)add typedef to your declaration 2)your function return an array (pointer) 3) move mypoint declaration outsize the loop and use malloc
typedef struct point
{
int x;
int y;
};
struct point* lineangle(int x1, int y1, int x2, int y2, int n){
double angle=360/n, s,c;
int rotated_x,rotated_y;
DDA(x1,y1,x2,y2);
int i;
struct point* mypoint = malloc(n*sizeof(struct point));
for(i=0;i<n;i++){
c = cos(angle*3.14/180);
s = sin(angle*3.14/180);
rotated_x= (x1 +((x2-x1)*c-(y2-y1)*s));
rotated_y= (y1 +((x2-x1)*s+(y2-y1)*c));
mypoint[i].x=x1;
mypoint[i].y=y1;
mypoint[i+1].x = rotated_x;
mypoint[i+1].y = rotated_y;
//DDA(x1,y1,rotated_x,rotated_y);
x2=rotated_x;
y2=rotated_y;
}
return mypoint;
}
Upvotes: 0
Reputation: 17472
The problem here is that you're declaring mypoint
inside of the for loop, so it's out of scope for the return value. Try moving the declaration to before the for loop.
struct point mypoint[]={};
for(int i=0;i<n;i++){
// ...
}
return mypoint;
Of course, that's not the only problem with your code. Honestly, I'm not sure exactly what you're trying to do here, but if you're declaring an array on the stack you also need to provide a length:
struct point mypoint[n + 1]={};
for(int i=0;i<n;i++){
// ...
}
return mypoint;
And mypoint
is an array of struct point
, not a single struct point
, but you are returning a single struct point
. Either return the whole array or return the element you want:
struct point mypoint[n + 1]={};
for(int i=0;i<n;i++){
// ...
}
return mypoint[0];
The other possibility is that you don't really want mypoint
to be an array, in which case you should just declare it as struct point mypoint;
(outside the loop). Maybe something like
struct point lineangle(int x1, int y1, int x2, int y2, int n){
double angle=360/n, s,c;
int rotated_x,rotated_y;
struct point mypoint;
DDA(x1,y1,x2,y2);
for(int i=0;i<n;i++){
c = cos(angle*3.14/180);
s = sin(angle*3.14/180);
rotated_x= (x1 +((x2-x1)*c-(y2-y1)*s));
rotated_y= (y1 +((x2-x1)*s+(y2-y1)*c));
struct point mypoint[]={};
mypoint.x = rotated_x;
mypoint.y = rotated_y;
// DDA(x1,y1,rotated_x,rotated_y);
x2=rotated_x;
y2=rotated_y;
}
return mypoint;
}
Upvotes: 1
Reputation: 426
You are declaring mypoint inside of the loop, it should be before the loop. In addition to this, the array also needs a size, if you have an undetermined size you might need a different datatype like a vector. Lastly, I think you should only be using point mypoint...
instead of struct point mypoint...
. What you currently have would create a struct of structs, however even this will not compile since you have already used the name “point” to define your original struct and you cannot use it again.
Upvotes: 1