0

i defined a matrix into a function. how do i return that matrix for print it when i call it with another function. i mean...

#include<stdio.h>
#include<conio.h>
#include<time.h>

void main() {
    int m,n;
    printf("type 2 numbers:");
    scanf("%i %i",&m,&n);
    declaration(m,n);\\HERE IS THE PROBLEM
    printing(matrix,m,n);
    getch();
}

void declaration(int a,int b) {
    srand(time(NULL));
    int i,j,matrix[a][b];
    for(i=0;i<a;i++){
        for(j=0;j<b;j++){
            matrix[i][j]=1+rand()%7;
        }
    }
}

void printing(int c[100][100],int a,int b) {
    int i,j;
    for(i=0;i<a;i++){
        for(j=0;j<b;j++){
            printf("%i\t",c[i][j]);
        }
        printf("\n");
    }
}
  • 2
    You cannot return a pointer to a an array declared local to a function with *automatic* storage. (the function stack is destroyed [memory released for reuse] on function return -- along with all local variables). Instead, you must allocate storage for the array in your function and return a pointer to the newly allocated block of memory (memory with *allocated* storage duration remains valid until it is `free`d -- or the program ends). – David C. Rankin Jul 04 '18 at 02:08
  • See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) for how to do it proper. There's a complete example at the bottom. – Lundin Jul 04 '18 at 10:58
  • OT: the function: `srand()` should ONLY be called once in the whole program. Usually very near the beginning of the `main()` function – user3629249 Jul 05 '18 at 22:27
  • OT: for ease of readability and understanding: 1) follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* 2) variable (and function parameter) names should indicate `content` or `usage` (or better, both) 3) insert a reasonable space: inside parens, inside braces, inside brackets, after semicolons, after commas, around C operators – user3629249 Jul 05 '18 at 22:31
  • the header file `conio.h` (and related library) are not portable. Strongly suggest not using any of the functionality in `conio.h` – user3629249 Jul 05 '18 at 22:32
  • the functions: `declaration()` and `printing()` are missing the needed prototypes before they are called. So the C compiler will make the assumption that the returned type and the parameters are `int` which is an error – user3629249 Jul 05 '18 at 22:34
  • OT: when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful – user3629249 Jul 05 '18 at 22:35
  • regarding: `void printing(int c[100][100],int a,int b) {` how did the matrix[][] variable suddenly become a 100x100 array? Strongly suggest the function signature be written as: `void printing( int rows, int columns, int matrix[ rows ][ columns ]) {` and the statement calling the function be modified accordingly and the parameter names be updated within the function: `printing()` – user3629249 Jul 05 '18 at 22:41
  • regarding: `int i,j; for(i=0;i – user3629249 Jul 05 '18 at 22:44
  • regarding: `printing(matrix,m,n);` the `matrix`, declared in `declaration()` is not visible in the `main()` function. Suggest returning a `int` pointer that points to the allocated memory for `matrix[][]` Then passing that returned pointer to `printing()` – user3629249 Jul 05 '18 at 22:48

3 Answers3

2

Define it like:

typedef struct {
    int rows;
    int cols;
    int *data;
} int_matrix_entity, *int_matrix;

int_matrix int_matrix_create(int rows, int cols, bool rand)
{
    int_matrix mt;
    int i;
    if ((mt = malloc(sizeof(int_matrix_entity))) == NULL)
    {
        return NULL;
    }

    if ((mt->data = malloc(sizeof(int) * cols * rows)) == NULL)
    {
        free(mt);
        return NULL;
    }

    if (rand)
    {
        srand(time(NULL));
        for (i = 0; i < cols * rows; i++)
        {
            mt->data[i] = 1 + rand() % 7;
        }   
    }
    else
    {
        memset(mt->data, 0, sizeof(int) * cols * rows);
    }

    return mt;
}

void int_matrix_printf(int_matrix mt)
{
    int i;
    int j;
    for (i = 0; i < mt->rows; i++)
    {
        for (j = 0; j < mt->cols; j++)
        {
            printf("%5d ", mt[i * cols + j]);
        }
        printf("\n");
    }
}
Joy Allen
  • 402
  • 3
  • 8
2

You have a few points that require a bit more attention;

1 ) read warning and error messages given by your compiler

2 ) again, read warning messages given by your compiler

3 ) use indentation to make your code more readable.

4 ) Always return from main(), that's a good practice

The code below does what you want to achieve; have a look at it and keep on reading...

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

// You either have to declare your functions 
// or implement them before main()
void declaration(int a,int b, int m[a][b]);
void printing(int a,int b,  int m[a][b]);

int main(){ // always return from main()
  int m,n;
  printf("type 2 numbers:");
  scanf("%i %i",&m,&n);
  int matrix[m][n];

  declaration(m, n, matrix);
  printing(m, n, matrix);

  return 0;
}

void declaration(int a,int b, int m[a][b]){
  srand(time(NULL));
  int i,j;

  for(i=0;i<a;i++){
    for(j=0;j<b;j++){
        m[i][j]=1+rand()%7;
    }
  }
}

void printing(int a,int b, int m[a][b]){
  int i,j;
  for(i=0;i<a;i++){
    for(j=0;j<b;j++){
        printf("%i\t",m[i][j]);
    }
    printf("\n");
  }
} 

You need a way to transfer data from one function to another. You cannot simply declare an auto variable in one function and pass it to another as you did in the code below

declaration(m,n);
printing(matrix,m,n); /* where does matrix[][] come from? */

remember, C is a strongly typed language which means you have to declare your variables before using them. This applies to your functions as well. You either have to give your function declarations before main() (or more specifically, before using them), or implement them.

Look into your header files (i.e. .h files) and you will see lots of function declarations.

Since you use variable length arrays, make sure your compiler is at least capable of compiling code confirming C99 standard.

Some extras;

Normally, C passes arguments by value and you have to use a pointer if you want the value of your variable get changed within the function. If you have a close look at the code snippet I gave, I simply used an int m[a][b].In C, the name of an array is a pointer to its first element, hence you can change the value of array elements when actually array's name is passed to your function as an argument.

For further reading, you may want to look at

  • variable scope
  • global variables (you can define matrix[][] as a global variable and change the value of matrix elements)
  • declaration vs definition in C
fnisi
  • 1,181
  • 1
  • 14
  • 24
0

Another simple way to do it is use double pointer to create 2-dimensional array. Keep it simple.

#include <stdio.h>
#include <stdlib.h>

int** create_matrix(int rows, int cols) {
    int **matrix = malloc(rows*(sizeof(int *)));
    for(int i = 0; i < rows; i++) {
        matrix[i] = malloc(cols*sizeof(int));
    }
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            matrix[i][j] = 1 + rand()%7;
        }
    }
    return matrix;
}

void printing(int** matrix, int rows, int cols) {
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main(void) {
    int rows, cols;
    rows = 3, cols = 3;
    int** matrix = create_matrix(rows, cols);
    printing(matrix, rows, cols);
    free(matrix);
    return 0;
}
Srikanth Chekuri
  • 1,944
  • 1
  • 9
  • 19
  • I'd hardly call this keep it simple. Keeping it simple would be to allocate a 2D array instead of a look-up table. [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Jul 04 '18 at 10:56