简体   繁体   English

如何使用指针在函数中传递二维静态数组(C)

[英]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.我试图通过传递给函数来创建一个静态二维数组,在那里我指定n条记录,然后读取它。 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?我应该写什么而不是a来使它工作?

Do I pass a correct type into the function?我是否将正确的类型传递给函数? (*matrix, int * matrix) (*矩阵, int * 矩阵)

Maybe what you are looking for is a Variable Length Array VLA.也许您正在寻找的是可变长度数组 VLA。
The function vla allocates the array on the stack so it's size is limited.函数vla在堆栈上分配数组,因此它的大小是有限的。 The array is initialized to zero and passed to fillvla .该数组被初始化为零并传递给fillvla
For simplicity, fillvla assigns a value to each element.为简单起见, fillvla为每个元素分配一个值。 value is declared static so on subsequent calls for this example the value increases. value被声明为static因此在此示例的后续调用中,该值会增加。
vla then prints the contents of the array and returns.然后vla打印数组的内容并返回。 Upon return, matrix no longer exists so the next iteration in main can call vla with a different dimension.返回时, matrix不再存在,因此main的下一次迭代可以调用具有不同维度的vla

#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.函数CreateStatic接受一个指向整数的指针作为参数,而不是一个二维数组。 In case you wanted to dynamically allocate the bidimensional array inside the function, you would have to declare it as int **matrix;如果您想在函数内部动态分配二维数组,则必须将其声明为int **matrix; and pass it to the function like this CreateStatic(matrix, &n);并将其传递给像CreateStatic(matrix, &n);这样的函数CreateStatic(matrix, &n); , then call malloc properly inside the function. ,然后在函数内部正确调用malloc 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);相反,如果您想用固定数量的列和行声明它,则需要在调用像CreateStatic(matrix[][NMAX], &n);这样的函数时指定它有多少列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 .对于函数CreateStatic初学者,您使用的是未声明的变量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] .用作函数调用参数的数组matrix被转换为指向数组第一个元素(行)的指针,其类型为int ( * )[NMAX]

So the function CreateStatic will look the following way所以函数CreateStatic将如下所示

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.在此函数定义中,假设 n 仅指定行数。 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如果您希望n指定行数和列数,则可以按以下方式更改内循环

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在 main 函数中可以像这样调用

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

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

CreateStatic( matrix, n );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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