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?