简体   繁体   中英

Passing multidimensional arrays by void functions

This is the code :

#include <stdio.h>

int funk1()
{
int N=0;
float a;
FILE *f;
f=fopen("wyniki.txt", "r");
while(fscanf(f,"%f", &a)!=EOF)
{
    N++;
}
fclose(f);
return N;
}

void funk2(int N, float T[][N])
{
FILE *f;
f=fopen("wyniki.txt", "r");
float a;
int i=0;
while(fscanf(f,"%f", &a)!=EOF)
{
    T[i][0]=a;
    T[0][i]=a;

    printf("funk 2\n");
    printf("liczba = %f    i = %d \n", a, i);
    printf("%f   %f\n", T[i][0], T[0][i]);


    i++;
}
fclose(f);

}

main()
{
int N =  funk1();
float T[N][N];

funk2(N,T[][N]);

int i=0;
int j=0;

for(i=0;i<N;i++){
    for(j=0;j<N;j++){
        printf("|%f|\t",T[i][j]);
    }
    printf("\n");
    printf("|%f|\t", T[i][j]);
}

getch();
return 0;
}

The problem is only with funk2. I want to pass the matrix (T) from main to funk2, do stuff with it and then give it back to main. I have no idea how to do that to be honest, I just tried out messing with it but it doesn't work. Any idea how to do that? Should I provide more information or is this enough? You can ignore the first function and the first half of funk2 as well. It all works just fine, the problem I have only has to do with passing the twodimensional array.

The result of this code is an error in

    funk2(N,T[][N]);

(expected expression before ']' token).

Adding an N there results in

expected 'float (*)[(sizetype)(N)]' but argument is of type 'float'

The only time [] is valid syntax is when declaring an array either with an initializer or as a function parameter.

If you want to pass an array to a function, just pass it:

funk2(N,T);

You don't need to call out the dimensions of the array. The compiler knows it's an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM