Reputation: 1264
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).
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 & columns of the array. This is where I am stuck.
Currently gcc is giving me error invalid type argument of ‘unary *’ for line -> rows = *i;
My code is:
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", &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 & columns for matrix 1
printf("Enter number of rows: ");
scanf("%d", &i);
printf("\nEnter number of columns: ");
scanf("%d", &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<i; x++) {
for (y=0; y<j; y++) {
int value_entered;
scanf("%d", &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<i; x++) {
for (y=0; y<j; y++) {
printf("%4d", matrix_input[x][y]);
}
printf("\n");
}
return array_pointer;
}
Any help would be appreciated.
Upvotes: 2
Views: 528
Reputation: 224844
i
is an int
. You don't need to dereference it. You do need to dereference rows
, though - *rows = i
will work just fine. Don't dereference when calling your function, though. Use:
matrixInput(rows, columns);
since you want to pass the pointers.
Upvotes: 3
Reputation: 1
int *matrix1[0][0], *matrix2[0][0];
this is strange and incorrect (you should not declare global variables as 0-dimensionned arrays).
And your code is not complete. You forgot at least the #include <stdio.h>
and also #include <stdlib.h>
And your code don't compile.
You should try to compile with all warnings enabled, e.g. on Linux with GCC with
gcc -Wall -g cud.c -o cud
I'm getting 4 errors and more than 10 warnings.
Don't post a code till you've got no warnings and no more errors. Edit & improve your code till all errors and warning are gone. Then debug your code (with a debugger, like gdb
on Linux).
Upvotes: 2