0

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.

6
  • 1
    Select either the C tag or the C++ tag. Commented Nov 27, 2019 at 23:18
  • Does this answer your question? C++ Return Array of Structs Commented Nov 27, 2019 at 23:29
  • @VladfromMoscow why did you re-tag this to C? The title says C++. Commented Nov 27, 2019 at 23:31
  • @Chipster Because he is always using the keyword struct with the structure type identifier. It seems it is a C code. Commented Nov 27, 2019 at 23:33
  • @VladfromMoscow while this does seem like C, it could be because a former C programmer is programming in C++. Unfortunately, the title is all we have to go on right now. Commented Nov 27, 2019 at 23:43

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

It's just to explain how to fix the problem they asked about, not all the other problems in the code (I see several).
How so? AFAICT it just doesn't fix the existing problems (other than the one they were asking about)… all I did was move the declaration outside the loop.
Note: The redeclaring the struct point could also be a problem in C++.
@nemequ You may not declare an array with unspecified number of elements and zero initializers.
Which isn't a new problem. The same problem existed in the original code.
1

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.

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.