Thread: arrays in function prototypes

  1. #1
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79

    arrays in function prototypes

    ok, relatively simple. I am declaring a function prototype, and I am trying to pass a 2-dimensional array. my error says "too few arguments to function 'int bsize(char (*) [16], int)' my actual code says "int bsize (char board[][16], int tile). Exactly how do I fix this?
    This has been a public service announcement from GOD.

    111 1111

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Where do you call it? That is where the problem will be.

    When you pass an array, it is the same as passing a pointer (to the first element of the array), so the error message is consistent with your code.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    I declare it before my int main()
    This has been a public service announcement from GOD.

    111 1111

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Where's the actual function call though (seeing code would help).

    My guess is the problem is something like this:
    Code:
    int bsize(char [][16], int);
    ...
    int main( )
    {
      char** str = ...;
      int z = bsize(str); // Note the missing parameter.. i.e. "Too few arguments to bsize(char (*)[16], int)"
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    *how* are you calling the function? It should be something like:

    Code:
    void foo(char m[][10]){}
    
    int main()
    {
     char x[10][10];
     foo(x);
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    Code:
    #include<iostream>
    #include<ctime>
    using namespace std;
    
    int bsize(char board[][16], int tile);
    int config(char board[][16], int tile);
    bool attack(char board[][16], int tile);
    void print(char board[][16], int tile);
    
    int main()         //need function to test for attacking queen, and to print
    {                  //out config
      srand(time(0));
      bsize();
      config(char board[][16], int tile);
      print(char board[][16], int tile);
    
      return 0;
    }
    
    
    int bsize()
    {
      int tile;
      char board[][16];
      cout<<"Please input a number. This number will\nrepersent the number of rows and columns.\nEx. inputting 3 means three rows AND three columns.";
      cin>>tile;
      return (board[][16], tile);
    }
    
    int config(char board[][16], int tile)
    {
      int queen, random, row=1, column=1;
      do
        {  
          do
    	{
    	  random=rand()%tile+1;
    	  board[row][random]='Q';
    	  row++;
    	}while(row<=tile);
          do
    	{
    	  random=rand()%tile+1;
    	  board[random][column]='Q';
    	  column++;
    	}while(column<=tile);
        }while(attack()=0);
      return (board[][16], tile);
    }
    bool attack(char board[][16], int tile)
    {
      int a=(-1), b=(-1), c=(-1), d=0, e=0, f=0, g=0, h=0, i=0, j=0;
      bool attack;
      do
        {
          if(board[i+a][j+b] && board[i+c][j+d])
    	{
    	  attack=1;
    	}
          e++;
          f++;
          if(e<=3)d++;
          if(e<=6 && e>=4)
    	{
    	  c++;
    	  if(f==3)
    	    {
    	      d=(-1);
    	      h++;
    	    }
    	}
          if(e<=9 && e>=7)
    	{
    	  b++;
    	  if(h==3)
    	    {
    	      c=(-1);
    	      g++;
    	    }
    	}
          if(e<=12 && e>=10)
    	{
    	  a++;
    	}
        }while(attack==0);
      return attack;
    }
    void print(char board[][16], int tile)
    {
      int alpha=0, omega=0, count=0, line=0;
      do
        {
          cout<<board[alpha][omega];
          line++;
          if(line==tile)cout<<endl;
          alpha++;
          if(alpha==tile)
    	{
    	  omega++;
    	  alpha=0;
    	}
        }while(omega<tile);
    }
    This has been a public service announcement from GOD.

    111 1111

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Take a look at the function's signature in the declaration, and then its signature in the definition. The problem should readily present itself.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    too me it looks like it's right. shouldn't it be the same in both??? if not coreect me. I've been working for a long time, and I may not be thinking correctly.
    This has been a public service announcement from GOD.

    111 1111

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Hang on... I didn't look closely enough to notice some other errors. Your two declarations should be the arguments. Also, you can only return one value. You can pass those parameters by reference and modify them, or you can return a structure containing a char** and an int, but you can only return one thing from the function (and it expects that thing to be an int).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    if I pass by reference, would that fix it. I only ask because I've seen other code that passes more than one arguement.
    This has been a public service announcement from GOD.

    111 1111

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The main problem I'm seeing is this:
    Code:
    int bsize(char board[][16], int tile);
    //... Later on ...
    int bsize( )
    {
      ...
    }
    You are correct that they should be the same.

    It actually appears that you don't need anything in your parameter list for this function. It never modifies its arguments, or otherwise uses them. It just needs to return its value that it got from the user.

    Also, neither board nor tile have are in scope in main. You need to define them before you pass them to functions.

    i.e.
    Code:
    int main( )         //need function to test for attacking queen, and to print
    {                  //out config
      srand(time(0));
      int tile = bsize( );
      char board[16][16] = { ... };
      config(board, tile);
      print(board, tile);
    
      return 0;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    in my situation I'm not allowed to use global variables. I'm trying your corrections now.
    This has been a public service announcement from GOD.

    111 1111

  13. #13
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    ok, I am still recieving that error in each instance of the array.
    This has been a public service announcement from GOD.

    111 1111

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> char board[][16];

    You can't declare an array like that. The compiler has no way of knowing how big to make the array.

    >> print(char board[][16], int tile);

    You don't invoke a function in the same way you declared it.

    Look at my previous post. That's basically how you declare an array and pass it to a function.


    >> int bsize(char board[][16], int tile);

    >> bsize();

    The function prototype takes two arguments. The definition and call take none. Which is it?

    Ditto for attack().

    >> return (board[][16], tile);

    What in the world are you doing here?

    >> bool attack;

    You need to initialize variables when necessary. Looking at the loop, I'd say it's necessary here.

    There could be some more errors in there. Those were just the most obvious.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    *edit: What he said ^.

    In the code I posted, they aren't global. They are function scope for main. They actually have to be outside any function to be global.

    What it seems is that you only want one instance of the board (array), and then pass it around to the other functions as necessary.

    bsize ought to look something like this at this point:
    Code:
    int bsize()
    {
      int tile;
      cout << ...;
      cin >> tile;
      return tile;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM