Thread: Very Confused. Record Structure.

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    75

    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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    75
    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. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

    Try your compiler, but I have a concern about *recs.
    Last edited by Adak; 02-15-2014 at 02:27 AM.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    75
    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. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    75
    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[50];
    
                    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. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    75
    BTW, is

    Code:
    RectT a, b, *recs;
    RectT rectangle[50];
    recs = rectangle;
    the most efficient way to write that? I feel like I may be repeating over myself.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could put a, b, *recs, and rectangle[50], all on the same line of code, since they are all RecT's data type.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data structure to record football season game results
    By javaeyes in forum C Programming
    Replies: 20
    Last Post: 11-25-2012, 05:45 PM
  2. confused with structure in function
    By Chris_1980 in forum C Programming
    Replies: 4
    Last Post: 04-26-2010, 12:24 PM
  3. how to make function return value go to structure record
    By Tom Bombadil in forum C Programming
    Replies: 1
    Last Post: 05-27-2009, 03:38 PM
  4. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  5. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM