简体   繁体   中英

How to pass 2-d static array in a function using pointers (C)

I'm trying to create a static 2-d array via passing in to the function, where I specify n number of records and then read it. Need to use pointers.

#define NMAX 100
void CreateStatic(int* matrix, int* n);

int main() {
    // int num;
    int matrix[NMAX][NMAX], n;
    CreateStatic(*matrix, &n);
}

void CreateStatic(int* matrix, int* n) {
    scanf("%d", n)
    for (int i = 0; i < *n; i++) {
        for (int j = 0; j < *n; j++) {
            scanf("%d", a);
        }
    }
}

What should I write instead of a to make it work?

Do I pass a correct type into the function? (*matrix, int * matrix)

Maybe what you are looking for is a Variable Length Array VLA.
The function vla allocates the array on the stack so it's size is limited. The array is initialized to zero and passed to fillvla .
For simplicity, fillvla assigns a value to each element. value is declared static so on subsequent calls for this example the value increases.
vla then prints the contents of the array and returns. Upon return, matrix no longer exists so the next iteration in main can call vla with a different dimension.

#include <stdio.h>

void vla( int dim);
void fillvla( int dim, int matrix[][dim]);

int main() {
    // int num;
    for (int dim = 2; dim < 6; dim++) {
        vla( dim);
    }
}

void vla( int dim) {
    int matrix[dim][dim];//vla valid in this function
    for (int row = 0; row < dim; row++) {
        for (int col = 0; col < dim; col++) {
            matrix[row][col] = 0;
        }
    }
    fillvla ( dim, matrix);//vla can be passed to a function
    for (int row = 0; row < dim; row++) {
        for (int col = 0; col < dim; col++) {
            printf ( "%2d ", matrix[row][col]);
        }
        printf ( "\n");
    }
    printf ( "\n");
}

void fillvla( int dim, int matrix[][dim]) {
    static int value = 0;
    for (int i = 0; i < dim; i++) {
        for (int j = 0; j < dim; j++) {
            matrix[i][j] = value++;
        }
    }
}

Function CreateStatic accepts a pointer to integer as parameter, not a bidimensional array. In case you wanted to dynamically allocate the bidimensional array inside the function, you would have to declare it as int **matrix; and pass it to the function like this CreateStatic(matrix, &n); , then call malloc properly inside the function. If you, instead, wanted to declare it with a fixed number of columns and rows, you need to specify how many cols it has when you're calling the function like this CreateStatic(matrix[][NMAX], &n); , since the function needs to know at least the number of columns of it.

For starters within the function CreateStatic you are using undeclared variable a .

 scanf("%d", a);

This call

scanf("%d", n);

should be moved from the function in main. That is the function should be called with already specified number of rows in the matrix.

The array matrix used as an argument of a function call is converted to a pointer to the first element (row) of the array and has the type int ( * )[NMAX] .

So the function CreateStatic will look the following way

void CreateStatic( int ( *matrix )[NMAX], size_t n ) 
{
    for ( size_t i = 0; i < n; i++ ) 
    {
        for ( size_t j = 0; j < NMAX; j++ ) 
        {
            scanf( "%d", &matrix[i][j] );
        }
    }
}

In this function definition it is supposed that the n specifies only the number of rows. If you want that n specified the number of rows and the number of columns simultaneously then you can change the inner loop the following way

void CreateStatic( int ( *matrix )[NMAX], size_t n ) 
{
    for ( size_t i = 0; i < n; i++ ) 
    {
        for ( size_t j = 0; j < n; j++ ) 
        {
            scanf( "%d", &matrix[i][j] );
        }
    }
}

And in main the function can be called like

int matrix[NMAX][NMAX];
size_t n;

if ( scanf( "%zu", &n ) != 1 || NMAX < n ) n =  NMAX;

CreateStatic( matrix, n );

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