Can't pass array to function

This is a discussion on Can't pass array to function within the C Programming forums, part of the General Programming Boards category; In this exercise there are 4 sales people and 5 products. I'm supposed to figure out how much of each ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Can't pass array to function

    In this exercise there are 4 sales people and 5 products. I'm supposed to figure out how much of each product each sales person sold in a month. initSales randomly generates the sales into transaction[][][] and succeeds in doing so. The problem I'm having is with totalSales which is supposed to total all the sales for a month for every product a person sold in a month. Whenever I try to compile the program I get,

    6.22.c: In function `main':
    6.22.c:23: warning: passing arg 1 of `totalSales' from incompatible pointer type
    6.22.c:23: warning: passing arg 2 of `totalSales' from incompatible pointer type

    What am I doing wrong?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define PERSON 5
    #define PRODUCT 6
    #define DAY 31
    
    void initSales( int [][][] );
    void totalSales( int [][], int [][][] );
    
    
    
    
    int main()
    {
       int transaction[ PERSON ][ PRODUCT ][ DAY ] = { 0 };
       int sales[ PERSON ][ PRODUCT ] = { 0 };
    
       srand( time(NULL) );
    
       initSales( transaction );
       totalSales( transaction, sales );
    
    
       return 0;
    }
    
    
    
    
    
    void initSales(int var[][ PRODUCT ][ DAY ])
    {
       int i, j, k;
    
    
       for (i = 1; i <= PERSON - 1; ++i) {
          for (j = 1; j <= PRODUCT - 1; ++j) {
             for (k = 1; k <= DAY - 1; ++k) {
                var[i][j][k] = rand() &#37; 1000;
                /* test statement below */
    //          printf("person: %d\nproduct: %d\n day: %d\nsold: %d\n\n", i, j, k, var[i][j][k]);
             }
          }
       }
    
    return;
    }
    
    
    
    
    
    void totalSales( int sumofSales[][ PRODUCT ], int transactionsMade[][ PRODUCT ][ DAY ] )
    {
       int a, b, c;
    
    
       for (a = 1; a <= PERSON - 1; ++a) {
          for (b = 1; b <= PRODUCT - 1; ++b) {
             for (c = 1; c <= DAY - 1; ++c) {
                sumofSales[ a ][ b ] += transactionsMade[ a ][ b ][ c ];
                /* test statement below */
                printf("person: %d\nproduct: %d\nsold: %d\n\n", a, b, sumofSales[ a ][ b ]);
             }
          }
       }
    
    
    return;
    }
    Last edited by yougene; 08-18-2007 at 11:26 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    It looks like totalSales( transaction, sales ) should be totalSales( sales, transaction ).

    Incidentally, it probably does not really matter, but you might want to initialise transaction to:
    Code:
    {{{0}}}
    And sales to:
    Code:
    {{0}}
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    nevermind
    Last edited by simpleid; 08-18-2007 at 11:49 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > void initSales( int [][][] );
    > void totalSales( int [][], int [][][] );
    These prototypes need the sizes of the minor dimensions, just like the definitions of the functions.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    be 4d? just a guess, seems like you're putting an array, in to the third d of this array... giving a 4th d.
    No, just as:
    Code:
    int x[5] = {0};
    Initialises a 1d array to all zeroes, and does not somehow turn it into a 2d array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    182
    >>laserlight
    I did have them backwards. That fixed it right up

    >>Salem
    I was able to run the program without the minor dimesnsions in the prototype. Is there any other reason for this besides redundancy?


    Thank you everyone.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Oh you mean other than the fact that you got the parameters the wrong way round?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    182
    Yeah, I don't see the relation, would it give me a more verbose error message or something?

  9. #9
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by yougene View Post
    Is there any other reason for this besides redundancy?
    This is everything but redundancy. There's a difference between a 3D char array (char foo[][X][Y]) and a "triple char pointer" (char foo[][][] or, written in an equivalent way, char ***foo).

    Your compiler should at least give you a warning.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    182
    Ahhh, I haven't gotten to pointers yet. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Is it possible to pass an array to a function?
    By Loic in forum C++ Programming
    Replies: 20
    Last Post: 05-06-2007, 10:04 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. pass the pointer of a two dimension array to a function
    By SoFarAway in forum C Programming
    Replies: 8
    Last Post: 04-13-2005, 05:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21