# Thread: Very Confused. Record Structure.

1. ## Very Confused. Record Structure.

Hello, I have a couple of questions on what I may be doing wrong. I always come to this forum and get great answers whenever I'm stuck so I'm hoping the community can help me out yet again.

So, I am making a record structure and playing around a little with passing it through a function (calling by reference) and making an array of the structure to run through the function as well.

The first problem (or confusion if you will) I am running into is where the instructions say to:

Write a function int chk_overlap(RectT *r1, RectT *r2) which can be called to check if a and b overlap or not. If so return 1, else return 0.
Now, when I just have the function with the parameters there I am getting an error message of:

missing ) before *
My lines of code declaring the struct:

Code:
```typedef struct Rect
{
double x;
double y;
char color;
double w; ///width
double h; //height
} RectT;

RectT a, b, *recs;```
My function:

Code:
```int chk_overlap(RectT *r1, RectT *r2){

if(r1 == r2)
return 1;

return 0;

}```

The second problem arrives when I am instructed to:

Dynamically create an array of 50 rectangles and save their address in recs. Then randomly initialize x, y, w, h of each rectangle (e.g., recs[i].x = rand()%20; etc...)
Using lines:

Code:
``` int i;
for(i = 0; i < 50; i++){
recs[i].x = rand()%20;
recs[i].y = rand()%20;
recs[i].w = rand()%20;
recs[i].h = rand()%20;
}```
lets me compile but gives a seg fault when trying to store/print each item, so I'm guessing using recs[?] cannot be done after using *recs.

I have been able to store in a.x, a.y... etc., but I'm having trouble when 1) passing struct through the function and 2) creating the array of structures. In no means am I posting hoping to receive the answer, as I feel a lot of people think judging the responses I sometimes will get, but getting any guidance on a little mishap here or there, someone else catching a mistake I made in my code, or if I am missing the concept of what I am doing and being told what I don't understand can really help get me over the hump. I am a student going through college and trying to learn, bashing really does nothing for anyone. 2. I beg to differ, Roadrunner - it makes me feel SOO GOOOD! <j/k>

problem #1:

1) Is the struct definition, above main(), before any other functions? If not, make it so.

2) Move the declaration of the structs down into main(). They should be passed as local data structures, not global. Global variables are typically abused by beginners, and even cause severe migraines for more experienced coders.

And post the code where you call the function, and pass the structs, if you still have trouble with it. 3. I beg to differ, Roadrunner - it makes me feel SOO GOOOD! <j/k> 1) Is the struct definition, above main(), before any other functions? If not, make it so.
Gahh I knew this was a possibility. I looked all through my notes + the professors notes and couldn't determine if it went outside or in.

Code:
```typedef struct Rect
{
double x;
double y;
char color;
double w; ///width
double h; //height
} RectT;

int main(int argc, char *argv[]){

RectT a, b, *recs;```
yes?

I haven't yet got to the point where im calling the function as Im correcting all my errors within the function and main itself. 4. It's smart to take it step by step, compiling every few lines to check the syntax. You CAN have structs defined inside a function, but then you can't easily make an instance of the struct, inside the other functions - like maybe in a sorting function, where you want a temp struct when swapping structs, etc. It's just VERY handy that way, and it pays to be consistent. 5. Thank you very much! I just completed the whole program and I wanted to come back to give a BIG thanks to helping me out. I understand what you're saying now about being able to/not being able to use the struct in other functions depending on where I initialize it and that's something I'll remember. What you helped me with really did push me over the hump to get this done and again I give a big thanks! Kudos! 6. Code:
``` typedef struct Rect
{
double x;
double y;
char color;
double w; ///width
double h; //height
} RectT;

int main(int argc, char *argv[]){

RectT a, b, *recs;
RectT rectangle;

recs = rectangle;```
Fixed my seg. fault.

Code:
``` int i;
for(i = 0; i < 50; i++){
recs[i].x = rand()%20;
recs[i].y = rand()%20;
recs[i].w = rand()%20;
recs[i].h = rand()%20;
}```
Now working like a charm.

Code:
``` int chk_overlap(RectT *r1, RectT *r2){

if( (r1->x + r1->w) < (r2->x) || (r2->x + r2->w) < (r1->x) )            // if the rectangle is too far to the right or left to not possibly match up, return 0
return 0;
if( (r1->y + r1->h) < (r2->y) || (r2->y + r2->h) < (r1->y) )            // if the rectangle is too far above or below to not possibly match up, return 0
return 0;

return 1;
}```
Reads in perfect now.  7. BTW, is

Code:
```RectT a, b, *recs;
RectT rectangle;
recs = rectangle;```
the most efficient way to write that? I feel like I may be repeating over myself. 8. You could put a, b, *recs, and rectangle, all on the same line of code, since they are all RecT's data type. 9. Code:
```int i;
for(i = 0; i < 50; i++){
recs[i].x = rand()%20;
recs[i].y = rand()%20;
recs[i].w = rand()%20;
recs[i].h = rand()%20;
}```
Should it be possible for a rectangle's width or height to be 0? Computing some random value modulus another number might give you 0's. Logically, I would think that these two values should never be 0 but you may allow for it. Popular pages Recent additions 