0

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)

2

3 Answers 3

1

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++;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

``` matrix[][NMAX]; ``` Could I ask you how does this construction work as a function parameter?
Here you go.
0

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 );

3 Comments

Should j < NMAX be j < n?
@IanAbbott I suppose that n specifiers the number of rows in the array. The number of columns is fixed.
But the original code was trying to input a square matrix. Only the top left corner of the 2D array is used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.