i have written the below program for matrix mult using pointers and functions.
the code is: - #include<stdio.h>
-
int M = 3,N = 2;
-
main()
-
{
-
int mat1[M][N],mat2[N][M],i,j,res[M][M];
-
void matmul(int *a,int *b,int *c);
-
printf("\n enter the matrix elements row wise the size is %dx%d \n",M,N);
-
for (i = 0;i < M;i++)
-
for (j = 0;j < N;j++)
-
scanf("%d",&mat1[i][j]);
-
printf("\n enter the second matrix elements of size %dx%d \n",N,M);
-
for (i = 0;i < N;i++)
-
for (j = 0;j < M;j++)
-
scanf("%d",&mat2[i][j]);
-
matmul(&mat1[0][0],&mat2[0][0],&res[0][0]);
-
printf("\n Matrix 1 = \n");
-
for (i = 0;i < M;i++)
-
{
-
for (j = 0;j < N;j++)
-
printf("%d \t",mat1[i][j]);
-
printf("\n");
-
}
-
printf("\n Matrix 2 = \n");
-
for (i = 0;i < N;i++)
-
{
-
for (j = 0;j < M;j++)
-
printf("%d \t",mat2[i][j]);
-
printf("\n");
-
}
-
printf("\n Result = \n");
-
for (i = 0;i < M;i++)
-
{
-
for (j = 0;j < M;j++)
-
printf("%d \t",res[i][j]);
-
printf("\n");
-
}
-
}
-
-
void matmul(int *a,int *b,int *c)
-
{
-
int i,j,k,d = sizeof(int);
-
printf("\n size of int = %d \n",d);
-
for (i = 0;i < M;i++)
-
{
-
for (j = 0;j < M;i++)
-
{
-
*(c + i * d + j) = 0;
-
//*(*(c + i) + j) = 0;
-
for (k = 0;k < N;k++)
-
*(c + i * d + j) += (*(a + i * d + k)) * (*(b + k * d + j));
-
//*(*(c + i) + j) += (*(*(a + i) + k)) * (*(*(b + k) + j));
-
}
-
}
-
}
-
im starting pointer exercises, and executing the pgm in fedora 12.
if i execute the above pgm, i get segmentation fault.
if i execute using the commented lines in the func, i get the foll error:
ex28.c: In function ‘matmul’:
ex28.c:49: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
help in debugging the errors or mistakes
6 3723
The calling aruments of your matmul function are int*. This is not a 2D array but is only the address of a single int.
There is a correct way to pass the address of a 2D array to a function and that is to pass the address of element 0. In a 2D array element 0 is itself an array so you need to pass the address of the array that is the second dimension.
This array:
is passed to a function using the argument of a pointer to an array of 4 int: - void MyFunc(int (*arg)[4]), int num);
You also need to pass in the number of elements. In this case the number of 4 int arrays.
Read this: http://bytes.com/topic/c/insights/77...rrays-revealed - #include<stdio.h>
-
int M = 3,N = 2;
-
main()
-
{
-
int mat1[M][N],mat2[N][M],i,j,res[M][M];
-
void matmul(int (*a)[N],int (*b)[M],int (*c)[M]);
-
printf("\n enter the matrix elements row wise the size is %dx%d \n",M,N);
-
for (i = 0;i < M;i++)
-
for (j = 0;j < N;j++)
-
scanf("%d",&mat1[i][j]);
-
printf("\n enter the second matrix elements of size %dx%d \n",N,M);
-
for (i = 0;i < N;i++)
-
for (j = 0;j < M;j++)
-
scanf("%d",&mat2[i][j]);
-
matmul(mat1,mat2,res);
-
printf("\n Matrix 1 = \n");
-
for (i = 0;i < M;i++)
-
{
-
for (j = 0;j < N;j++)
-
printf("%d \t",mat1[i][j]);
-
printf("\n");
-
}
-
printf("\n Matrix 2 = \n");
-
for (i = 0;i < N;i++)
-
{
-
for (j = 0;j < M;j++)
-
printf("%d \t",mat2[i][j]);
-
printf("\n");
-
}
-
printf("\n Result = \n");
-
for (i = 0;i < M;i++)
-
{
-
for (j = 0;j < M;j++)
-
printf("%d \t",res[i][j]);
-
printf("\n");
-
}
-
}
-
-
void matmul(int (*a)[N],int (*b)[M],int (*c)[M])
-
{
-
int i,j,k,d = sizeof(int);
-
printf("\n size of int = %d \n",d);
-
or (i = 0;i < N;i++)
-
for (j = 0;j < M;j++)
-
{
-
printf("%d \t",(*(a + i))[j]);
-
}
-
for (i = 0;i < M;i++)
-
{
-
for (j = 0;j < M;i++)
-
{
-
(*(c + i))[j] = 0;
-
for (k = 0;k < N;k++)
-
(*(c + i))[j] += (*(a + i))[k] * (*(b + k))[j];
-
}
-
}
-
}
-
ok i changed the pgm for passing the array parameters.but i still get segmentation fault. and cant print the array in the func using pointers
and cant print the array in the func using pointers
?
If you can't see the output it's because crash occurs before the output stream is flushed. As for seg fault itself, the reason may be an array index out of bound - then icorrect pointer is fetched and when it's accessed, crash occurs. Did you localize it?
Your code compiled and executed OK on Visual Studio.NET 2008. I didn;t check that the answer was correct but therewas no crash.
I did have to make M and N const and I did have to make main() return an int.
It crashes because 'i' runs out of range.
its working now thanx for ur replies,heres the working pgm:
#include<stdio.h>
int M = 3,N = 2;
void main()
{
int mat1[M][N],mat2[N][M],i,j,res[M][M];
void matmul(int ar1[][N],int ar2[][M],int ar3[][M]);
printf("\n enter the matrix elements row wise the size is %dx%d \n",M,N);
for (i = 0;i < M;i++)
for (j = 0;j < N;j++)
scanf("%d",&mat1[i][j]);
printf("\n enter the second matrix elements of size %dx%d \n",N,M);
for (i = 0;i < N;i++)
for (j = 0;j < M;j++)
scanf("%d",&mat2[i][j]);
matmul(mat1,mat2,res);
printf("\n Matrix 1 = \n");
for (i = 0;i < M;i++)
{
for (j = 0;j < N;j++)
printf("%d \t",mat1[i][j]);
printf("\n");
}
printf("\n Matrix 2 = \n");
for (i = 0;i < N;i++)
{
for (j = 0;j < M;j++)
printf("%d \t",mat2[i][j]);
printf("\n");
}
printf("\n Result = \n");
for (i = 0;i < M;i++)
{
for (j = 0;j < M;j++)
printf("%d \t",res[i][j]);
printf("\n");
}
}
void matmul(int ar1[][N],int ar2[][M],int ar3[][M])
{
int i = 0,j = 0,k = 0,d = sizeof(int),(*a)[N] = ar1,(*b)[M] = ar2,(*c)[M] = ar3;
printf("\n size of int = %d \n",d);
printf("\n size of int = %d \n",d);
for (i = 0;i < M;i++)
{
for (j = 0;j < M;j++)
{
(*(c + i))[j] = 0;
printf("%d \t",(*(c + i))[j]);
}
}
for (i = 0;i < M;i++)
{
for (j = 0;j < M;j++)
{
(*(c + i))[j] = 0;
for (k = 0;k < N;k++)
{
(*(c + i))[j] += (*(a + i))[k] * (*(b + k))[j];
}
}
}
return;
}
'i' was going out of range cos i was incremented in the j loop also. newbie mistake.the lesson was very good for arrays to function parameter passing.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: christopher diggins |
last post by:
Here is some code I wrote for Matrix multiplication for arbitrary
dimensionality known at compile-time. I am curious how practical it is. For
instance, is it common to know the dimensionality of...
|
by: Tiza Naziri |
last post by:
Hi,
How to represent this matrix equation in C code:
/ a0 \ / 0 1 0 1 0 0 1 0 \/ b0 \
| a1 | | 0 0 1 0 1 0 0 1 || b1 |
| a2 | | 1 0 0 1 0 1 0 0 || b2 |
| a3 |=| 0 1 0 0 1 0 1 0 || b3 |
| a4...
|
by: Frank-O |
last post by:
Hi ,
Recently I have been commited to the task of "translating" some complex
statistical algorithms from Matlab to C++. The goal is to be three
times as fast as matlab ( the latest) .
I've...
|
by: lituncse |
last post by:
dear friends,
i have come across a problem which is difficult to solve for me.it's about starssen's matrix multiplication.in general matrix multiplication we need 8 multiplications and 4 additions...
|
by: John Doe |
last post by:
segmentation error !!!!
hi guys ,
i wrote this program to multiply two matrices (just the basic code
without checkin 4 the condition n==p )
"
#include<stdio.h>
main()
{
int a,b,c;
int...
|
by: VijaKhara |
last post by:
Hi all,
Is there any method which can implememt the matrix multiplication
faster than using the formula as we often do by hand?
I am writing the following code and my matrice: one is 3x40000 and...
|
by: sadegh |
last post by:
Hi
I have a problem with my program in VC++6
When I compile it, the following errors are listed. I spend a lot of
time on the groups.google.com to find its reason, but none of comments
could...
|
by: dcatunlucky |
last post by:
Ok, I have an assignment to write a program that multiplies two matrices. The matrices dimensions will be user defined as well as the numbers (floating point values) inside of them. The program must...
|
by: Sozos |
last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help.
#define index(i,j,power) (((i)<<(power))+(j))
void...
|
by: joegao1 |
last post by:
can some one give me a hint?
I want to program the code for matrix multiplication with as less arithmetical / multiplication operations as possible.
my task is to calculate the matrix...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |