Thread: passing arrays into functions

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    7

    passing arrays into functions

    hey guys so i have a program that works, but now I am trying to get function1() to work. What it has to do is bring in the array and populate it with random letters. I don't know much about functions, so I am kind of clueless about the errors that I am getting..

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    #define maxrow 20  //defines maxrow as a constant of 20
    #define maxcol 30  //defines maxcol as a constant of 30
    
    void function1(char array[][]);
    void function4(int, int); //function to display the pairs count
    
    int main( void )
    {  // MARKS THE BEGINNING OF THE main( ) BLOCK OF STATEMENTS
    
    
    int x = 0;
    int y = 0;
    int row = 0;
    int col = 0;
    int countX = 0;
    int countY = 0;
    srandom( (unsigned) time(NULL) );
    char array[maxrow][maxcol];
    char array[x][y];
    
    /*
    for (x=0;x<maxrow;x++)
        {
         for (y=0;y<maxcol;y++)
            {
             array[x][y] = random() % 26 + 'A';
             printf("%c ", array[x][y]);
            }
        printf("\n");
        }
    printf("\n");
    */
    function1(array[x][y]);
    
    
    for (row=0;row<maxrow-1;row++)
        {
         for (col=0;col<maxcol; col++)
            {
             if (array[row][col] == array[row+1][col])
               {
                countX++;
               }
            }
        }
    
    for(col=0;col<maxcol-1;col++)
        {
         for (row=0;row<maxrow;row++)
            {
             if (array[row][col] == array[row][col+1])
               {
                countY++;
               }
            }
        }
    
    
    //calls function4 to display the countX and countY
    function4 (countX, countY);
    
    
        return ( 0 ) ;
    
    }   
    
    char function1(array[x][y])
    	{
    int x = 0;
    int y = 0;
    
    	 for (x=0;x<maxrow;x++)
        {
         for (y=0;y<maxcol;y++)
            {
             array[x][y] = random() % 26 + 'A';
             printf("%c ", array[x][y]);
            }
        printf("\n");
        }
    printf("\n");
    
    return(array[x][y]);
    	}
    
    
    void function4(int countX, int countY)
        {
        printf("\nNumber of horizontal pairs: %d\n", countX);
        printf("\nNumber of vertical pairs: %d\n\n", countY);
         }
    
    the errors when i compile:
    Code:
    test.c:8 error: array type has incomplete element type
    test.c: In function 'main':
    test.c:23: error: redeclaration of 'array' with no linkage
    test.c.22: error: previous declaration of 'array' was here
    test.c.37: error: type of formal parameter 1 is incomplete
    test.c: At top level:
    test.c:71 error: expected ')' before '[' token

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    void function1(char array[][]);
    Only the left-most set of brackets can be empty in function parameters. That is the only dimension of the array that "decays to a pointer" when passed. The rest must have the correct number in the dimensions.
    Code:
    char array[maxrow][maxcol];
    char array[x][y];
    Two variables with the same name in the same scope is not allowed. Rename one.
    Code:
    char function1(array[x][y])
    You need to provide a type for array. What is it an array of? int? char?

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You can also do the following to declare your function which will accept any size 2d array

    Code:
    void function1(size_t rows, size_t cols, char array[rows][cols]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arrays to functions
    By cda67 in forum C Programming
    Replies: 3
    Last Post: 11-05-2011, 03:37 AM
  2. passing 2d arrays to functions
    By Tool in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2010, 10:26 PM
  3. Passing Arrays to Functions...
    By txcs in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2009, 10:36 AM
  4. Passing Arrays into functions
    By manutdfan in forum C Programming
    Replies: 23
    Last Post: 11-14-2006, 01:25 PM
  5. passing arrays to functions
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-01-2002, 03:18 PM

Tags for this Thread