Thread: Structure arrays...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    15

    Structure arrays...

    Can it be done? And if so, would this be appropiate if my structure was already created?

    Code:
    patch mystructures[10];
    patch p;
    patch *pptr;
    pptr = &p;
    .
    .
    .     
    .
    .
    .
    void get_patches(patch *p){
        count = 0;
        .
        .
        .
        mystructures[count] = p;    //this is what I am really referring to.
    }
    Last edited by krsauls; 05-01-2007 at 09:56 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    except for the last assignment, where you assign pointer to struct seems to be OK

    last function should be
    Code:
    void get_patches(patch *p)
    {
         count = 0;
         mystructures[count] = *p;
    }
    don't use glabals - and you will not have such mixes...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    15
    Alright. That sounds good to me. Thank you.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    15
    How about this?

    Can I use a pointer in my structure and get it to point to an array that has been declared?

    My struct


    Code:
    typedef struct {
            char name[NAME];
            int height;
            int width;
            char *patch;    //my curiosity is peaked   
    }patch;
    and the code I have tried:

    Code:
    char thearray[p->height][p->width];
    p->patch = &thearray;
    I am sure that I am overlooking something...

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    typedef struct{
       ...
       char *patch;
    } patch;  /* possible naming conflict?  I'd pick something different. */
    
     char thearray[p->height][p->width];
    The array that you want to point to has another dimension now. You will want to consider
    char **patch;
    or char (*patch)[MAX_WIDTH];
    which will make indexing the array easier on you.

    or flattening the original array like &thearray[0][0];
    but some arithmatic to dereference the correct address is necessary.
    MAX_HEIGHT * this_height + this_width
    I think.

    You'd want to go with one of the first two unless you trust my math skills.
    Last edited by whiteflags; 05-01-2007 at 10:59 PM. Reason: forgot the parens on something. oops.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    15
    You'd want to go with one of the first two unless you trust my math skills.
    Hell, I trust them more than mine! HA. I will give it a go...thanks.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    15
    OK. Here is the complete function. Things are fine until I try to fill my array with the patches from the file input. I will repost that here:
    Code:
    rect
    7 9
    +-------+
    |       |
    |       |
    |       |
    |       |
    |       |
    +-------+
    ex
    5 5
    *   *
     * * 
      *  
     * * 
    *   *
    dollar
    1 1
    $
    the rect line is loaded into the struct just fine, as are the dimensions(7 9). At line 3, I am using the dimensions as my height and width to fill my two dimensional array, which is in my structure.
    The problem is in my for loop logic I am sure, but I cannot seem to locate it:
    Code:
    void get_patches(patch *p){
         char line[100];
         int count = 0;
         
         while(fgets(line, sizeof(line), patches) != NULL) {
             //name
             strcpy(p->name, line);
                      
             //height and width
             fscanf(patches,"%d %d", &p->height, &p->width);
                      
             //get the patch into an array
             int i;
             int j;
             for (i = 0; i < p->height; ++i){
                 fgets(line, sizeof(line), patches); 
                 for (j = 0; j < p->width; ++j){
                     p->patch[i][j] = line[j]; 
                     
                     //debug
                     printf("%c", p->patch[i][j]);
                 }
             }
             system("PAUSE");
    
             //place the structure into an array 
             mystructures[count] = *p;
             count++; 
         }
    
    }
    It does not seem to be breaking the for loop in the correct spot throwing the rest of the program in disarray and eventually crashing. Here is some sample output from before it crashes:

    Code:
    File List:
         patches1.txt
         patches2.txt
         patches3.txt
         commands1.txt
         commands2.txt
         commands3.txt
    
    
    
    Input a patches filename:  patches1.txt
    
     ct
       &#241;+-------+|       ||       ||       ||       ||       |Press any key to continue . . .
    ex
     ----+5 5
     ---+*   *
     -+ * *
     -+  *
     -+ * *
     -+*   *
     -+Press any key to continue . . .
    
    Press any key to continue . . .
    $Press any key to continue . . .
    As you can see, the for loop is grabbing and placing the next name and dimensions, whereas it's not supposed to. It is supposed to break at the end of the patch so the main control loop can start in the correct position. Can anyone see my mistake?
    Last edited by krsauls; 05-01-2007 at 11:18 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Don't mix fgets and fscanf in the same program.

    Replace the fscanf call with a call to fgets, then use sscanf on the buffer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    15
    Quote Originally Posted by Salem View Post
    Don't mix fgets and fscanf in the same program.

    Replace the fscanf call with a call to fgets, then use sscanf on the buffer.
    You da man. That got it...I guess my logic works after all, eh?

    Hey, fellas, I really appreciate it. It helps to have a second eye. I'll have to buy y'all a beer sometime.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You're very welcome. I may take you up on that offer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structure arrays
    By mapunk in forum C Programming
    Replies: 8
    Last Post: 11-17-2005, 08:44 AM
  2. Structure Within Structure
    By Shakira in forum C Programming
    Replies: 3
    Last Post: 11-04-2003, 03:35 PM
  3. sorting a structure of arrays
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 11:45 AM
  4. displaying data from a structure of arrays
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 03-14-2002, 12:35 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM