Reputation: 253
I am trying to add two matrices using the following code:
#include <stdio.h>
int matrix (void)
{
int a[2][2];
int b[2][2];
int c[2][2];
int i, j;
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
return c;
}
int main ()
{
int i, j;
int c[2][2];
c = matrix();
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
printf("%d", c[i][j]);
}
printf("\n");
}
return 0;
}
Above code is throwing many errors. Please explain why so many errors are shown by my compiler.
EDIT- I used GCC 4.4.3 when compiling above code.
EDIT2 - I got following errors and warnings when compiling above code.
test.c: In function ‘matrix’:
test.c:23: warning: return makes integer from pointer without a cast
test.c:23: warning: function returns address of local variable
test.c: In function ‘main’:
test.c:29: error: incompatible types when assigning to type ‘int[2][2]’ from type ‘int’
Upvotes: 1
Views: 151
Reputation: 535
Your're returning 2D-Array by using return c which can only be used to return a variable having data type other than 1D or 2D-Array. To fix this, you need to return 2D-Array i.e
return [i][j]
Also, you've declared c as 2D-Array but while returning you're using it as a variable.
int c[2][2];
c = matrix();
To fix it, either use
c[i][j]= matrix();
or declare c as
int c;
Upvotes: -1
Reputation: 6057
Try this fixed code:
#include <stdio.h>
void matrix (int c[][2]) //Fix 1
{
int a[2][2];
int b[2][2];
int i, j;
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
}
int main ()
{
int i, j;
int c[2][2];
matrix(c); //Fix 2
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
printf("%d\t", c[i][j]); //Fix 3
}
printf("\n");
}
return 0;
}
Upvotes: 2
Reputation: 40145
Fix as what you want.
#include <stdio.h>
int (*matrix(void))[2]{//return pointer as int (*)[2]
static int c[2][2];//Can not be used to return the automatic local variables.
int a[2][2];
int b[2][2];
int i, j;
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
return c;
}
int main (){
int i, j;
int (*c)[2];
c = matrix();
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
printf("%d ", c[i][j]);//remove &
}
printf("\n");
}
return 0;
}
Upvotes: 2
Reputation: 11949
The following will not print the value:
printf("%d", &c[i][j]);
But some pointer (if it compile). You must do:
printf("%d", c[i][j]);
Upvotes: 0
Reputation: 49803
For one thing, you have matrix
returning a local array of int
s, but declared it to return a single int
value.
Upvotes: 2