-5

Im passing a matrix as argument in a function call saveFile

void saveFile(char raw[],char inf[],int r, int c, int m[r][c]){

   FILE *save_raw,*save_inf;        
   int i,j;

   save_raw = fopen(raw,"w");
   save_inf = fopen(inf,"w");

    for(i=0;i<r;i++){
       for(j=0;j<c;j++){
           fprintf(save_raw,"%d ",m[i][j]);     
       }

       fprintf(save_raw,"\n");
    }

And Im calling the fuction like this

saveFile(image_name_extension,image_name_info,rows,columns,raw_n);

The output of raw_n before calling the function is:

0 132 132 246 237
0 223 132 246 237
0 132 132 246 237
230 132 132 246 237
0 166 142 246 248

but for some reason I dont now the output of raw_n inside the saveFile function is:

0 132 132 246 237
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

I'ts not a logical problem, anybody can help me? Thanks.

raw_n is filled like this

for(i=0;i<rows;i++){
    for(j=0;j<columns;j++){
        raw_n[i][j] = MAX_PIXEL - raw[i][j];
    }
}
Carlos Gomez
  • 79
  • 3
  • 10

1 Answers1

3

In comments you have agreed that the array passed is defined similarly to

int n_raw[MAX_ROWS][MAX_COLS];

with both dimensions as 500. But you have defined the function as

void saveFile(char raw[],char inf[],int r, int c, int m[r][c])

with arguments r = 5 and c = 5. The function believes what you have told it, and accesses the array incorrectly. The only information the function has about the array is what is in the function arguments. The array is not rearranged to suit the subset you want to access. The crucial information is the number of columns in the array, without which the compiler is unable to align each row.

I suggest you change the function definition to

void saveFile(char raw[],char inf[],int r, int c, int m[MAX_ROWS][MAX_COLS])

or even

void saveFile(char raw[],char inf[],int r, int c, int m[][MAX_COLS])
Weather Vane
  • 33,872
  • 7
  • 36
  • 56