Note that there are some explanatory texts on larger screens.

plurals
  1. POPointers in C, returning from functions?
    primarykey
    data
    text
    <p>I am trying to create a simple matrix multiplication program in C. To do this I have opted to use 2 functions: 1 to simply create matrices, and 1 to hold all the created matrices (for doing multiplication).</p> <p>I have finally managed to work out how to pass an array from a function (or more specifically pass a pointer to the array from a function), however, I want the function matrixWelcome() below to be passed the number of rows &amp; columns of the array. This is where I am stuck.</p> <p>Currently gcc is giving me error invalid type argument of ‘unary *’ for line -> rows = *i;</p> <p>My code is:</p> <pre><code>void matrixWelcome() { int selection; int a, b, c, d, *rows, *columns; int *matrix1[0][0], *matrix2[0][0]; printf("Welcome to matrix mode\n"); printf("Please select from the following: \n"); printf("1 - Matrix multiplication"); scanf("%d", &amp;selection); switch (selection) { case 1: printf("Selected matrix multiplication...\n"); printf("Please enter matrix 1\n"); matrix1[*rows][*columns] = matrixInput(*rows, *columns); printf("%d\n", *matrix1[1][1]); a = *rows; b = *columns; printf("****ROWS = %d, COLUMNS = %d", a, b); // printf("Matrix 1 has %d rows and %d columns", rows, columns); printf("Please enter matrix 2\n"); // matrix2 = matrixInput(); break; default: printf("No input entered\n"); break; } } int *matrixInput(int *rows, int *columns) { int i, j, x, y; int *array_pointer = malloc(5 * sizeof(int)); //grab some memory if (array_pointer == NULL) { // check that we have successfully got memory return NULL; } // Number of rows &amp; columns for matrix 1 printf("Enter number of rows: "); scanf("%d", &amp;i); printf("\nEnter number of columns: "); scanf("%d", &amp;j); rows = *i; columns = *j; // Initialise 2D array to hold values int matrix_input[i][j]; printf("Enter values for matrix, starting at 1,1 and moving across 1st row; then move across 2nd row etc.."); // loop to store values in matrix for (x=0; x&lt;i; x++) { for (y=0; y&lt;j; y++) { int value_entered; scanf("%d", &amp;value_entered); matrix_input[x][y] = value_entered; } } *array_pointer = matrix_input[i][j]; // print out matrix - just to confirm printf("You've entered the following matrix: \n"); for (x=0; x&lt;i; x++) { for (y=0; y&lt;j; y++) { printf("%4d", matrix_input[x][y]); } printf("\n"); } return array_pointer; } </code></pre> <p>Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload