-1

am building a simple program dealing with structs. The logic is simple enough but for some reason I can't quite figure out how to pass the struct to a function. I declared the struct in my main(). I got the impression from searching the site that the only way to do this is to create a header file and declare that way. Is this true?

 main() {

    struct Rect {
            double x;
            double y;
            char color;
            double width;
            double height;

    };



    struct Rect a, b, *rec;

and this is where I am trying to pass it:

int chk_overlap(struct Rect *r1, struct Rect *r2) {
    if(((r1->x  + r1->width) >= r2->x) && ((r1->y) >= (r1->y - r2->height))){
            return 1;
    } else {
            return 0;
    }
}

This is just one iteration of my attempt, when I pass it like this, I get a dereferencing to incomplete pointer error. I have also tried declaring it as

typedef struct Rect {
            double x;
            double y;
            char color;
            double width;
            double height;

    } Rect;

Rect a, b, *rec;

passing it as

int chk_overlap(Rect *r1, Rect *r2) {

EDIT: This is where I am actually using the function

        int check = 0;
    check = check + chk_overlap(&a, &b);
  • 1
    Could you also post the code where you're actually using the function? It's likely the problem is there. – AntonH Apr 28 '14 at 16:43
  • @AntonH Yes sorry, just edited that. – Avery Pfeiffer Apr 28 '14 at 16:46
  • Also, you say you declared the struct `in my main()`. Do you mean in the same file as the main, or in the body of the main? Because you should declare it before, if it's the second (as in: `struct Rect {...} ... int main (...) {Rect &, b, *rec; ...}`). – AntonH Apr 28 '14 at 16:46
  • How are you deferencing in chk_overlap? – brumScouse Apr 28 '14 at 16:48
  • @AntonH I have edited to show the entire function that I am passing to, including how I dereference. Also I edited to show exactly where I declare the struct, inside of the main() function – Avery Pfeiffer Apr 28 '14 at 16:53

1 Answers1

1

You should declare the structure before the main.

struct Rect {
    double x;
    double y;
    char color;
    double width;
    double height;
}

/* Place here the function definitions */

int main (int argc, char *argv[]) {
    struct Rect a, b, *rec;
    ...
}

/* place here the function code */

Since you're declaring it inside the main() it's not seen outside, so functions don't recognise it.

Other than that, the way you're calling the function (function(&a, &b)) looks correct.

AntonH
  • 6,359
  • 2
  • 30
  • 40
  • Thank You! That seems to have worked! So for future reference, you structs declared outside of mains don't need a 'global' attached to them? – Avery Pfeiffer Apr 28 '14 at 16:58
  • @AveryPfeiffer You're welcome. And to answer your question: No. Declaring a struct inside a function (such as `main`) means they're only available inside that function. Declaring them at the beginning, or inside another file that is included, makes them "global". – AntonH Apr 28 '14 at 17:00