Thread: Very Confused. Record Structure.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

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