Quick Array Problem

This is a discussion on Quick Array Problem within the C Programming forums, part of the General Programming Boards category; I have the following code: Code: #include <stdio.h> #include <stdlib.h> int max_rows; int max_columns; void print(char[][]); int main(int argc, char ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    Quick Array Problem

    I have the following code:

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int max_rows;
    int max_columns;
    void print(char[][]);
    
    int main(int argc, char *argv[]){
    	
    	max_rows = 3;
    	max_columns = 3;
    	char grid[max_rows][max_columns];
    	
    	for(int i = 0; i < max_rows; i++){
    		for(int j = 0; j < max_columns; j++){
    			grid[i][j] = 'X';
    		}
    	}
    	
    	print(grid);
    	
    }
    
    void print(char grid[max_rows][max_columns]){
    	
    	
    	for(int i = 0; i < max_rows; i++){
    		for(int j = 0; j < max_columns; j++){
    			printf("%c ", grid[i][j]);
    		}
    		printf("\n");
    	}
    	
    }
    I get the following errors:

    error: array type has incomplete element type (line: void print(char[][])
    In function ‘main’:
    error: type of formal parameter 1 is incomplete (line: print(grid)

    Any ideas???

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Compile in C99 mode, otherwise you can't do what you're trying to do. Also, multi-dimension arrays are converted to a pointer to their type. So an array of array of ints becomes a pointer to an array of ints. And, it doesn't like that you're trying to not tell it how big the array is in the function declaration.


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

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Could you possibly post an example of how to fix this?? I have to turn this in by midnight tonight and need to get it done. The reason I've set it up like this is because max_rows and max_columns will be specified by command line arguments.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Search the forums. Questions about creating dynamic arrays or dynamic multi-dimension arrays are pretty common.


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

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This version runs well on old compilers:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int max_rows;
    int max_columns;
    //we can pass this way without knowing the values of max rows and columns
    //we could change it, if we knew the sizes at this point
    void print(char**);
    
    int main(int argc, char *argv[]){
    	
    	char **grid;
       int i, j;
    	max_rows = 3;
    	max_columns = 3;
    
    
       *grid = malloc(max_rows * sizeof(char*));
    
       for(i = 0; i < max_rows; i++)
          grid = malloc(max_columns * sizeof(char));  //not a pointer size
       	
    	for(i = 0; i < max_rows; i++){
    		for(j = 0; j < max_columns; j++){
    			grid[i][j] = 'X';
    		}
    	}
    	
    	print(grid);
       //free each row
    	for(i = 0; i < max_rows; i++)
          free(grid[i]);
       //then free the whole array
       free(grid);
    
       printf("\n\n\t\t\t      press enter when ready ");
       i = getchar();
       return 0;
    }
    
    void print(char **grid) {
       int i, j;	
    	
    	for(i = 0; i < max_rows; i++){
    		for(j = 0; j < max_columns; j++){
    			printf("%c ", grid[i][j]);
    		}
    		printf("\n");
    	}
    	
    }
    Last edited by Adak; 04-03-2009 at 09:19 PM.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Solution

    Better you can check this program.It will work fine.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int max_rows;
    int max_columns;
    void print(char grid[]){
    int i,j;
    
            for( i = 0; i < max_rows; i++){
                    for( j = 0; j < max_columns; j++){
                            printf("%c ", grid[i]);
                    }
                    printf("\n");
            }
    
    }
    
    
    int main(int argc, char *argv[]){
    
            max_rows = 3;
            max_columns = 3;
            int i,j;
            char grid[max_rows][max_columns];
    
            for( i = 0; i < max_rows; i++){
                    for( j = 0; j < max_columns; j++){
                            grid[i][j] = 'X';
                    }
            }
    
            print(grid[0]);
    
    }
    good luck..

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by karthigayan View Post
    Better you can check this program.It will work fine.
    good luck..
    As was already stated it will not work in the C90 mode
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  3. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM

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