Syntax for functions

This is a discussion on Syntax for functions within the C Programming forums, part of the General Programming Boards category; I have a couple of errors in my code (which I'll shorten for reading convenience). I think it's merely because ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Syntax for functions

    I have a couple of errors in my code (which I'll shorten for reading convenience). I think it's merely because I'm still quite new to the way functions are expressed

    Code:
    void printBoard(int size, int align, int board[size][size]);
    
    int main(int argc, char *argv[]) {
        int board[MAX][MAX] = {{}}; // intialized elements to zero
        int size = 0;
        int align = 0;
        
       printBoard(size, align, board[MAX][MAX]);
    
       return 0;
    }
    
    void printBoard(int size, int align, int board[size][size]) {
        int i,j;
    
        for (i=0; i<size; i++) {
            for (j=0; j<size; j++) {
                printf("%*d", align, board[i][j]);
            }
        }
    }
    The lines with errors are in red. What am I doing wrong? And in this code I have that size <= MAX depending on the user input for size, so should I be sending board[size][size] to the printBoard function or should I be sending board[MAX][MAX] since my loops are only going to be dealing with a size*size array anyway.

    EDIT: The errors I'm getting are,
    for first line of error - note: expected ‘int (*)[(unsigned int)(size)]’ but argument is of type ‘int’
    second line - error: passing argument 3 of ‘printBoard’ makes pointer from integer without a cast
    Last edited by Mentallic; 08-25-2011 at 11:46 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    First since board has been defined as int board[MAX][MAX] your function signature should have board with the same size, not the user entered size.

    Second when you call the function you are calling like: printBoard(size, align, board[MAX][MAX]); The third argument should be a int board[MAX][MAX] but your call is sending a single int which also happens to be out of range for your array.

    So your code should look like:

    Code:
    #define MAX 100
    
    void printBoard(int size, int align, int board[MAX][MAX]);
    
    int main(int argc, char *argv[]) {
        int board[MAX][MAX] = {{}}; // intialized elements to zero
        int size = 0;
        int align = 0;
        
       printBoard(size, align, board);
    
       return 0;
    }
    
    void printBoard(int size, int align, int board[MAX][MAX]) {
        int i,j;
    
        for (i=0; i<size; i++) {
            for (j=0; j<size; j++) {
                printf("%*d", align, board[i][j]);
            }
        }
    }
    You may want to look at this link for a refresher on arrays. Pay particular attention to the section titled: "Arrays as parameters".

    In future posts please post the complete error message exactly as they appear in your development environment. These messages have important information embedded in them to aid in the discovery of the location and probable cause of the problem.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Oh thanks for all the help!

    I had no clue arrays were passed into functions without the brackets, and the link says that when I create a function, the array needs to be expressed as such:
    base_type[][depth][depth]
    So since I don't determine the value of size till later on in my code, I should leave the function as board[][MAX] and then pass int size into the functions as well, then I'll shorten my array to board[size][size] from there. That's what it seems like you've done.

    I would've posted the full error message if I didn't cut out most of my code, because the line number is useless in this case. Should I just post my full code and the complete error reports in future?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax for Declaring Functions in Dev-C++
    By Muz in forum C Programming
    Replies: 4
    Last Post: 07-03-2007, 04:53 AM
  2. Syntax Help, class interactions and functions..
    By Shamino in forum C++ Programming
    Replies: 18
    Last Post: 05-17-2007, 01:09 PM
  3. Replies: 3
    Last Post: 10-31-2006, 01:15 AM
  4. Geometry-shapes functions' syntax ?
    By bigjoke in forum C Programming
    Replies: 9
    Last Post: 01-01-2006, 09:25 PM
  5. Replies: 1
    Last Post: 01-20-2002, 10:50 AM

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