user2175838
user2175838

Reputation: 43

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

Upvotes: 0

Views: 340

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753605

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

Upvotes: 2

Related Questions