Two dimensional parameter in function

int test[visina][sirina]; //visina and sirina are not constant
flood(test, color, x, y); //i would like to send test array into function

Not working
void flood(int test[][], int color, int x, int y){
...
}

Not working

void flood(int test[visina][sirina], int color, int x, int y){
...
}

Working
void flood(int test[9][9], int color, int x, int y){
...
}

Any other ideas on how can I solve this?
Array sizes must be constant at compile time.

Check out some of the other STL containers which can have objects added dynamically.

Hope all goes well.
I have no idea how to fix this. Can someone plese check my code?

http://pastebin.com/Ta50SY4F
Ahh - you are doing C not C++ - you should have said.

Do you have any compile errors? if you do, always post them.

Line 32, 57 could be some of them :

TheIdeasMan wrote:
Array sizes must be constant at compile time.
With the code posted I dont get any errors couse it work with constant numbers but if I change line 5 to void flood(int test[visina][sirina], int color, int x, int y){ I get:

datoteka.c:5: error: 'visina' undeclared here (not in a function)
datoteka.c:5: error: 'sirina' undeclared here (not in a function)
datoteka.c: In function 'flood':
datoteka.c:8: error: type of formal parameter 1 is incomplete
datoteka.c:9: error: type of formal parameter 1 is incomplete
datoteka.c:10: error: type of formal parameter 1 is incomplete
datoteka.c:11: error: type of formal parameter 1 is incomplete
datoteka.c: In function 'main':
datoteka.c:73: error: type of formal parameter 1 is incomplete
Last edited on
I got the algortihm from here and he uses an array obr without calling it in a function, how can I do that? http://www.netgraphics.sk/floodfill-algorithm-2
Anyone please I need this quite fast.
One problem you have, is you are getting a solution written in java, and you are trying to convert it into C code.

The solution you have is probably part of a class where the obr is a class member.

there is a source code button on this page:

http://www.netgraphics.sk/floodfill-algorithm


Your link to your code is broken, there was not much code - just paste it here (remember the code tags).

To pass a 2d array to a function, you only need to specify the size of the second dimension. Declare the sizes of the array as a const int variable in the scope outside the function.

Also note that the solution you have uses recursion, your code doesn't. You also have much more code than the solution.

The array should be passed by reference with a pointer (the array name is a pointer), as opposed to passing by value which means copying the whole array.

Yeah but the second dimension changes, couse its the width of the file? I could do int test[][9] but if a file is of width 10 it wont work. I tried test[][sirina] but it doesn't work either. The code I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <string.h>
#include<stdlib.h>

 void flood(int test[9][9], int color, int x, int y){
    if(test[x][y] == 255 ){
        test[x][y] = color;
        flood(test,color,x+1,y);
        flood(test,color,x,y+1);
        flood(test,color,x-1,y);
        flood(test,color,x,y-1);
    }
}

int main(int argc, char * argv[]){
    int color = atoi(argv[3]); //barva s katero zamenjamo polje
    int x = atoi(argv[4]); //zaèetna x koordinata
    int y = atoi(argv[5]); //zaèetna y koordinata

    //najprej odpremo datoteko in preberemo vse v tabelo slika
    char prebrano[1000];
    char slika[1000][1000];
    int stevec = 0;
    FILE * f = fopen(argv[1],"r");
    while(fscanf(f, "%s", prebrano) != EOF){
        strncpy(slika[stevec],prebrano,1000);
        stevec++;
    }
    fclose(f);

    //zapišemo samo števila v tabelo vsaStevila
    char vsaStevila[stevec][1000];
    int s = 0;
    int i;
    for(i = 0; i < stevec; i++){
        if(atof(slika[i]) > 0 || strcmp(slika[i],"0") == 0){
            strncpy(vsaStevila[s],slika[i],1000);
            s++;
        }
    }

    //iz tabele dobimo sirino, visino in max barv
    int sirina = atoi(vsaStevila[0]);
    int visina = atoi(vsaStevila[1]);
    int maxBarv = atoi(vsaStevila[2]);

    //vsa ostala stevila zapisemo v drugo tabelo
    char stevila[s][1000];
    int st = 0;
    int j;
    for(j = 3; j < s; j++){
        strncpy(stevila[st],vsaStevila[j],1000);
        st++;
    }

    //iz teh stevil naredimo dvodimenzionalno tabelo, ki jo posljemo za floodfill
    int test[visina][sirina];
    int a;
    int stVrstic = 0;
    int stStolpec = 0;
    for(a = 0; a < st; a++){
        if(stStolpec < sirina){
            test[stVrstic][stStolpec] = atoi(stevila[a]);
            stStolpec++;
        }
        if(stStolpec == sirina){
            stVrstic++;
            stStolpec = 0;
            test[stVrstic][stStolpec] = atoi(stevila[a]);
        }
    }

    flood(*test, color, x, y);

    FILE * fi = fopen(argv[2],"w");
    fprintf(fi,"P2\n");
    fprintf(fi,"%d %d %d\n",sirina, visina, maxBarv);

    int b;
    int c;
    for(b = 0; b < visina; b++){
        for(c = 0; c < sirina; c++){
            fprintf(fi,"%d ",test[b][c]);
        }
        fprintf(fi,"\n");
    }
    fclose(fi);
}
Last edited on
I did it like this can you check if it is ok?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int width = 0;

 void flood(int test[][width], int color, int x, int y){
    if(test[x][y] == 255 ){
        test[x][y] = color;
        flood(test,color,x+1,y);
        flood(test,color,x,y+1);
        flood(test,color,x-1,y);
        flood(test,color,x,y-1);
    }
}

int main(int argc, char * argv[]){
    int color = atoi(argv[3]); //barva s katero zamenjamo polje
    int x = atoi(argv[4]); //zaèetna x koordinata
    int y = atoi(argv[5]); //zaèetna y koordinata

    //najprej odpremo datoteko in preberemo vse v tabelo slika
    char prebrano[1000];
    char slika[1000][1000];
    int stevec = 0;
    FILE * f = fopen(argv[1],"r");
    while(fscanf(f, "%s", prebrano) != EOF){
        strncpy(slika[stevec],prebrano,1000);
        stevec++;
    }
    fclose(f);

    //zapišemo samo števila v tabelo vsaStevila
    char vsaStevila[stevec][1000];
    int s = 0;
    int i;
    for(i = 0; i < stevec; i++){
        if(atof(slika[i]) > 0 || strcmp(slika[i],"0") == 0){
            strncpy(vsaStevila[s],slika[i],1000);
            s++;
        }
    }

    //iz tabele dobimo sirino, visino in max barv
    int sirina = atoi(vsaStevila[0]);
    int visina = atoi(vsaStevila[1]);
    int maxBarv = atoi(vsaStevila[2]);

    //vsa ostala stevila zapisemo v drugo tabelo
    char stevila[s][1000];
    int st = 0;
    int j;
    for(j = 3; j < s; j++){
        strncpy(stevila[st],vsaStevila[j],1000);
        st++;
    }

    //iz teh stevil naredimo dvodimenzionalno tabelo, ki jo posljemo za floodfill
    int test[visina][sirina];
    int a;
    int stVrstic = 0;
    int stStolpec = 0;
    for(a = 0; a < st; a++){
        if(stStolpec < sirina){
            test[stVrstic][stStolpec] = atoi(stevila[a]);
            stStolpec++;
        }
        if(stStolpec == sirina){
            stVrstic++;
            stStolpec = 0;
            test[stVrstic][stStolpec] = atoi(stevila[a]);
        }
    }
    width = sirina;
    flood(*test, color, x, y);

    FILE * fi = fopen(argv[2],"w");
    fprintf(fi,"P2\n");
    fprintf(fi,"%d %d %d\n",sirina, visina, maxBarv);

    int b;
    int c;
    for(b = 0; b < visina; b++){
        for(c = 0; c < sirina; c++){
            fprintf(fi,"%d ",test[b][c]);
        }
        fprintf(fi,"\n");
    }
    fclose(fi);
}
Last edited on
Did this compile?

You are still using variables to declare array sizes on lines 34, 50, 59. You cannot do this. if the variables concerned were defined as const at the start of the program, this would be OK.

In the call to the flood function, you dereference the name of the array - which gives the value at test[0] which is an int. So this conflicts with the declaration & definition of the function

When passing an array as a pointer, you have to use pointer arithmetic to access the contents. Read chapter 5 in this book:

http://zanasi.chem.unisa.it/download/C.pdf


If you are going to code in C you should really own this book.

Try to avoid magic numbers like 1000 on lines 23 & 24. declare them as const at the start:

const int SIZE= 1000;

then use them like this:

1
2
char prebrano[SIZE];
char slika[SIZE][SIZE];


HTH
C99 has support for variable length arrays.
So lets say I wanna get rid of a 1000 in line 23 and 24. How to use malloc or realloc to change the size of the array to optimal size. So I don't waste memory + if there are 1001 lines the program will still work.
Topic archived. No new replies allowed.