Thread: 2D structure arrays: passing to function, declaration Q:

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Michigan U.P.
    Posts
    20

    2D structure arrays: passing to function, declaration Q:

    This code appears to work fine. That - in spite of ongoing STRANGE 'scanf()' behavior. (to which I seem to have found a 'fix').

    I'm struggling with the function declaration:
    void show_motorcycledat(struct motorcycles (*)[2]);

    and use:
    void show_motorcycledat(struct motorcycles (*pmc)[2])

    Why not use both dimensions of the 2D array: "struct motorcycles (*)[3][2]" ?

    Further, I thought I'd be using "pmc[row][col]->" for member access rather than "pmc[row][col]." as seen in function body. Thanks.

    Code:
    #include <stdio.h>
    
    #define MAXROW  3
    #define MAXCOL    2
    
    #define SLENGTH       80
    enum {RET_ERR = -1,  RET_OK};
    
    struct motorcycles {
         char   name[25];
         int      size;    
         float  weight;
         };
    
    char G_inst[SLENGTH];
    
    void show_motorcycledat(struct motorcycles (*)[2]);
    
    int main(void)  {
         int                                 c;                                                         // VOODOO 'scanf()' fix
         int                                 entrycount;
         int                                 row;
         int                                 col;
    
         char                              letrs[MAXROW][MAXCOL];
         int                                 nums[MAXROW][MAXCOL];
         struct motorcycles mc[MAXROW][MAXCOL];
    
         printf("\nPlease enter %d lines.\n", MAXROW * MAXCOL);
         printf("Each line:  LETTER <sp> NUMBER <sp> Motorcycle length <sp> MC weight <sp> MC name\n\n");
    
         entrycount = 0;
         for (row = 0; row < MAXROW; row++)  {
              for (col = 0; col < MAXCOL;  col++)  {
                   printf("Entry %d:   ", ++entrycount);
                   scanf("%c %d %d %f %[^\n]", &letrs[row][col], &nums[row][col],
                        &mc[row][col].size, &mc[row][col].weight, mc[row][col].name);
    
                   while ((c = getchar()) != '\n' && c != EOF );        // VOODOO code for 'scanf()' fix
                   }          
              }
    
         entrycount = 0;
         printf("\nResults:\n\n");
         for (row = 0; row < MAXROW; row++)  {
              for (col = 0; col < MAXCOL;  col++)  {
                   printf("%d:  (R: %d . C: %d)  %c %d %d %.2f %s\n", ++entrycount, row, col, letrs[row][col], nums[row][col],
                        mc[row][col].size, mc[row][col].weight, mc[row][col].name);
                   }          
              }
    
         show_motorcycledat(mc);
    
         printf("\n");
         return(RET_OK);
         }
    
    void show_motorcycledat(struct motorcycles (*pmc)[2])   {
         int row;
         int col;
    
         printf("\nResults using struct *:\n\n");
         for (row = 0; row < MAXROW; row++)  {
              for (col = 0; col < MAXCOL;  col++)  {
                   printf("(R: %d . C: %d) %d %.2f %s\n", row, col, pmc[row][col].size, pmc[row][col].weight, pmc[row][col].name);
                   }          
              }
         }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,778
    Quote Originally Posted by gandsnut
    Why not use both dimensions of the 2D array: "struct motorcycles (*)[3][2]" ?
    Then you would have to deference the pointer just to get at the 2D array, whereas if you make use of the fact that an array is converted to a pointer to its first element when passed as an argument, it's simpler.

    Also, although not relevant here, that would constrain the function to taking only 2D arrays of that particular size, rather than 2D arrays with varying number of rows. (It's not relevant since the function uses MAXROW rather than taking the number of rows as an argument.)

    If you prefer, you could write:
    Code:
    void show_motorcycledat(struct motorcycles pmc[][MAXCOL])
    as it means the same thing.

    Quote Originally Posted by gandsnut
    Further, I thought I'd be using "pmc[row][col]->" for member access rather than "pmc[row][col]." as seen in function body.
    Why would you do that? You have a 2D array of struct motorcycles objects, not pointers.
    Last edited by laserlight; 05-15-2020 at 08:52 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structure arrays to function using pointers
    By KarlHendrikson in forum C Programming
    Replies: 2
    Last Post: 12-20-2015, 10:17 AM
  2. Replies: 1
    Last Post: 10-12-2011, 04:46 PM
  3. passing structure to function
    By danieldcc in forum C Programming
    Replies: 4
    Last Post: 10-10-2011, 11:17 AM
  4. Passing Function to Structure
    By C-bob in forum C Programming
    Replies: 11
    Last Post: 07-22-2011, 07:36 PM
  5. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM

Tags for this Thread