0

I have to make a function that creates points in the structure Points, I can get it to work if i manually add the points but when I try to make it so the user can add points then it stops working.

This appear every time I try to run my code:

n function ‘PrintPoint’:
error: subscripted value is neither array nor pointer nor vector
        printf("Point %d is in (%f,%f)\n", i, p[i].X, p[i].Y);
                                               ^
In function ‘createPoint’:
error: variable-sized object may not be initialized
    struct Point p[i] = {x, y};
           ^~~~~ 

This is my code:

/* a) creates the data structure */
struct Point {
    float X;
    float Y;
}p;
typedef struct Point points;
int main() {
    printf("This program will create an array and do all the points\n"); //What it does
    double x, y;
    int cant;
    /* c) create points */
    printf("Please insert the amount of points you wish to create\n");
    scanf("%d", &cant);
    struct Point p[cant];
    for (int i = 3; i < cant; i++){
        printf("X and Y\n");
        scanf("%d %d", &x, &y);
        points* createPoint(x, y, cant, i);
    }
    /* b) prints the points */
    PrintPoint(p);
    /* c) creates point function */
    return 0;
}

void PrintPoint (points p, int cant){
    for (int i = 0; i < cant; i++){
        printf("Point %d is in (%f,%f)\n", i, p[i].X, p[i].Y);
    }
}

points* createPoint (double x, double y, int cant, int i){
    struct Point p1 = {2.0, -3.0};
    struct Point p2 = {-4.0, 5.0};
    struct Point p[i] = {x, y};
}
Daina
  • 19
  • 4
  • Recommend enabling compiler warnings and fix everything reported. – Milag Nov 17 '20 at 17:04
  • As the error says, you can't initialise a VLA like that. Please see [C compile error: “Variable-sized object may not be initialized”](https://stackoverflow.com/questions/3082914/c-compile-error-variable-sized-object-may-not-be-initialized) – Weather Vane Nov 17 '20 at 17:07
  • If only I had thought of that! It's not like I spent far too much time trying to fix the errors and decided to come here for help becaus I couldn't find a way to fix them. – Daina Nov 17 '20 at 17:13
  • `for (int i = 3; i < cant; i++)` what does this condition do, i think you meant to use `for (int i = 0; i < cant; i++)` – IrAM Nov 17 '20 at 17:16
  • Because I'm suppose to have the first 2 points already made – Daina Nov 17 '20 at 17:59

0 Answers0