简体   繁体   中英

making a function with typedef struct with arrays

I was just wondering what I'm doing wrong here? The errors are mostly from my first function, am I calling it wrong?

typedef struct{ //typedef and function prototype
     int x, y, radius;
}circle;
int intersect(circle c1, circle c2);

part of the main function that I need for my function

circle c1 = {5, 6, 3.2};
circle c2 = {6, 8, 1.2};

returns 1 if its two circle arguments intersect. How do I call the arrays using struct properly? I keep getting errors

int intersect(circle c1, circle c2){

    float cx, cy, r, distance;
    cx = (circle c1[0].x - circle c2[0].x) * (circle c1[0].x - circle c2[0].x);
    cy = (circle c1[1].x - circle c2[1].x) * (circle c1[1].x - circle c2[1].x);
    r = (circle c1[2].x - circle c2[2].x);
    distance = sqrt(cx + cy);
    if (r <= distance){
        return 1;
    }else{
    return 0;
    }
}

I'm preparing for finals, so help will be appreciated

There are no arrays in your code, so don't try using array notation. Also, don't declare local variables that have the same names as the function parameters.

int intersect(circle c1, circle c2)
{
    float dx, dy, r, distance;
    dx = (c1.x - c2.x) * (c1.x - c2.x);
    dy = (c1.y - c2.y) * (c1.y - c2.y);  // x changed to y throughout
    r  = (c1.r + c2.r);                  // rewritten too
    distance = sqrt(cx + cy);
    if (r <= distance)
        return 1;
    else
        return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM