Thread: Passing 2d arrays to functions

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    Passing 2d arrays to functions

    Pleeaase help,
    I have the following code that when I run it in *.cpp file it compiles and works but when I compile it as *.c, I get the following errors:


    --------------------Configuration: defaultC - Win32 Debug--------------------
    Compiling...
    default.c
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(18) : error C2057: expected constant expression
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(18) : error C2466: cannot allocate an array of constant size 0
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(18) : error C2087: '<Unknown>' : missing subscript
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(19) : error C2057: expected constant expression
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(19) : error C2466: cannot allocate an array of constant size 0
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(19) : error C2087: '<Unknown>' : missing subscript
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(39) : warning C4048: different array subscripts : 'int (*)[1]' and 'int [3][4]'
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(39) : warning C4024: 'CreateMatrix' : different types for formal and actual parameter 1
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(45) : error C2057: expected constant expression
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(45) : error C2466: cannot allocate an array of constant size 0
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(45) : error C2087: '<Unknown>' : missing subscript
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(98) : error C2057: expected constant expression
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(98) : error C2466: cannot allocate an array of constant size 0
    c:\documents and settings\oren juster\my documents\visual studio projects\defaultc\default.c(98) : error C2087: '<Unknown>' : missing subscript
    Error executing cl.exe.

    defaultC.exe - 12 error(s), 2 warning(s)



    the code is:


    Code:
    #include <stdio.h>
    #include<malloc.h>
    
    typedef struct entry{
    	int column;
    	int value;
    	struct entry *next;
    }t_entry;
    
    typedef struct row {
    	int index;
    	t_entry *element;
    	struct row *next;
    }t_row;
    
    const int N = 4;
    
    t_row *CreateMatrix(int mat[][N], int row, int col);
    int check_if_sparce(int mat[][N], int row, int col);
    t_row *AddMatrices(t_row *a1, t_row *a2);
    
    int main()
    {
    	t_row *res;
    	int i,j;
    	int ctr = 0;
    	int mat[3][4];
    	
    	for (i=0;i<3;i++) 
    	{
    	    for (j=0;j<4;j++) 
    	    	mat[i][j] = 0;	   
    	}
    
    	mat[0][3]=3;
    	mat[2][0]=9;
    	mat[2][1]=5;
    	
    	res = CreateMatrix(mat,3,4);
    
    	
    	return 0;
    }
    
    t_row *CreateMatrix(int mat[][N], int row, int col)
    {
    	t_row *head=NULL;
    	t_row *curr_t_row=NULL;
    	t_entry *curr_t_entry=NULL;
    	int i,j;
    
    	if (!check_if_sparce(mat,row,col)) return NULL;
    
    	for (i=0;i<row;i++) 
    	{
    	    for (j=0;j<col;j++) 
    	    {
    	        if (mat[i][j])
    	        {
    				if (!head)
    				{
    					if((head = curr_t_row = (t_row*)malloc(sizeof(t_row))) == NULL)
    						return NULL;
    					head->index = i+1;
    					head->next = NULL;
    					if((head->element = curr_t_entry = (t_entry*)malloc(sizeof(t_entry)))==NULL)
    						return NULL;
    				}
    				else
    				{
    					if (i+1==curr_t_row->index) 
    					{
    						if((curr_t_entry->next = (t_entry*)malloc(sizeof(t_entry)))==NULL)
    							return NULL;
    						curr_t_entry = curr_t_entry->next;
    					} 
    					else 
    					{
    						if((curr_t_row->next = (t_row*)malloc(sizeof(t_row)))==NULL)
    							return NULL;
    						curr_t_row = curr_t_row->next;
    						curr_t_row->index = i+1;
    						curr_t_row->next=NULL;
    						if((curr_t_row->element = curr_t_entry = (t_entry*)malloc(sizeof(t_entry)))==NULL)
    							return NULL;
    					}
    				}
    
    				curr_t_entry->column = j+1;
    				curr_t_entry->value = mat[i][j];
    				curr_t_entry->next=NULL;
    	        }
    	    }
    	}
    	return head;
    }
    
    int check_if_sparce(int mat[][N], int row, int col)
    {
    	int zeroes = 0;
    	int i,j;
    
    	for (i=0;i<row;i++)
    	{
    		for (j=0;j<col;j++)
    		{
    			if (mat[i][j] == 0)
    				zeroes++;
    		}
    	}
    
    	if (70/(100/(row*col))>zeroes) 
    		return 0;
    	else 
    		return 1;
    }

    I think the problem is connected with passing the 2d array to the function, I must be doing something wrong. Can someone please explain this concept to me?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > #include<malloc.h>
    Use stdlib.h

    > const int N = 4;
    > t_row *CreateMatrix(int mat[][N], int row, int col);
    const has different meanings in C and C++.
    The only way to do this in C is using a #define

    Code:
    #define N 4
    t_row *CreateMatrix(int mat[][N], int row, int col);
    > (t_row*)malloc(sizeof(t_row))
    Don't cast malloc in C programs - see the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    thanks salem it worked!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Arrays to Functions...
    By txcs in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2009, 10:36 AM
  2. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  3. Passing arrays to functions?
    By gibbofresco in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2006, 11:10 AM
  4. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 08:08 AM
  5. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM