Thread: Arrays as method parameters?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    11

    Arrays as method parameters?

    We have this project, where one of the methods is called readStats. Basically, it reads numbers from a file. The method is:

    Code:
    int readStats( int month[], int day[], int auburn[], int opp[], int attend[] );
    We've had methods with int parameters, but I'm confused as to how exactly we call this method in main. It seems weird to me, since the arrays are already declared at the top, passing them wouldn't really seem to do much.

    Thanks for any suggestions!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays, when used as parameters to functions, actually change to a pointer to their first element. You may have learned that the name of an array may be used almost like a pointer to its first element. That is to say, it's not a pointer, but it can be referenced as one for some things. As such...
    Code:
    void somefun( int a1[], int a2[], int a3[] )
    {
       ...
    }
    
    ...
    int array1[ X ], array2[ Y ], array3[ Z ];
    
    ...
    somefun( array1, array2, array3 );
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    So to call it, I just put in the name of the array, with the size I want the array to be? Sorry, most array things have come naturally to me, but this just seems confusing. Thanks for the help!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not exactly. You will need to have some way to know when you've reached the end of the array though. So you might consider passing that along to the function as well.

    First you make an array. You have to provide at that time, the size of the array:
    Code:
    int array[ 5 ];
    This makes an array of 5 integers, available to us using indexes zero through four.

    We can pass it to a function using its name. If you want to pass one single value of the arrray to a function, you specify that location:
    Code:
    takesanarray( array );
    ...
    takesanint( array[3] );
    Now, you can specify the size of what your function expects for an array, but it doesn't really matter if it's a single dimension array:
    Code:
    void compilesfine( int array[ 10 ] )
    {
        /* do stuff */
    }
    int main( void )
    {
        int array[ 5 ];
        compilesfine( array );
        return 0;
    }
    This will compile without warnings or errors. The reason is, again, arrays as arguments are actually pointers to their type. So the function doesn't actually take "an array of 10 integers", it takes "a pointer to an int". It doesn't care how many integers I'm really passing it, so long as what it is receiving is an integer pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Okay, here's what I have so far, and it doesn't seem to be working. Is there a better debug method besides temp print lines? Thanks again!

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    int readStats( int month[], int day[], int auburn[], int opp[], int attend[] );
    void printReport( int month[], int day[], int auburn[], int opp[], int nGames ); 
    int  printDetails( int month[], int day[], int auburn[], int opp[], int nGames ); 
    void printHeader();  
    void printSummary( int auburn[], int opp[], int nGames, int nWin );  
    double mean( int array[], int nElements );  
    
    int main()
    {
    int MAX_GAMES = 100;
    int readStats( int month[MAX_GAMES], int day[MAX_GAMES], int auburn[MAX_GAMES], int opp[MAX_GAMES], int attend[MAX_GAMES] );
    }
    
    int readStats( int month[], int day[], int auburn[], int opp[], int attend[] )
    {
    FILE *tempFile;
    tempFile = fopen("2009_wbb_stats.txt", "r");
    
    int temp;
    
    fscanf(tempFile, "%d", &temp);
    printf("%d", &temp);
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The & operator is for getting the address of a variable. So, unless you mean to print the address of temp, you shouldn't be using it in your printf statement.

    Scattering printf statements is one of the easiest ways to help debug. Just make sure you've got them set up right, so you're printing the right thing.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Wow, what a beginner's mistake. I haven't done that in months. Anyways, is there any other reason you see it wouldn't be printing out anything? It could be my input file I guess...

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Check the return value of fscanf to see if the scan fails or not. Initialize temp to something, and print it before the fscanf call, just so you know everything is working fine. Typically I do:
    Code:
    printf( "Reading from file...\n" );
    ...call fscanf...
    printf( "read: %d\n", data );
    Also, unless you have a newline, there's no guarantee that printf will flush the output stream (print anything). So it could just be pending and crash before you see it, or whatever it's doing in your case.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Okay, so I put a printf function at the top of readStats, and it didn't run it...so apparently, I'm not calling it correctly.

    This is so frustrating, I've done all of my projects with ease, this one is kicking my butt.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Okay, after looking at it again, in my main it looks like I'm re-declaring readStats. I feel like I shouldn't have int in front of it, but when I remove it, it gives me a syntax error. Am I calling it incorrectly?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are just declaring (prototyping) it in main. You aren't actually calling it. To call a function, use its name, parenthesis, and any arguments it needs in those parenthesis. Plus, you haven't actually declared any of the variables you're showing as proposed arguments. So you'll need to do that too.

    Look again at my first and second posts.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Okay, so here's my next attempt, but it's still not working.

    Code:
    int MAX_GAMES = 100;
    int month[MAX_GAMES], day[MAX_GAMES], auburn[MAX_GAMES], opp[MAX_GAMES], attend[MAX_GAMES];
    readStats (month[] , day[], auburn[], opp[], attend[] );
    It also won't work without the [].

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Define "won't work".
    Code:
    #include<stdio.h>
    void foo( int array[], size_t sz )
    {
        size_t x;
        for( x = 0; x < sz; x++ )
            printf( "array[ %d ] is %d\n", x, array[ x ] );
    }
    int main( void )
    {
        int a[ 5 ] = { 5, 10, 3, 88, 0 };
        foo( a, 5 );
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  5. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM