By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

segmentation error ....!

P: n/a
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[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
printf("Enter the size of the first array A:");
scanf("%d %d",&m,&n);

printf("Enter the size of the second array A:");
scanf("%d %d",&p,&q);

printf("\nEnter the value of the Matrix A:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("\nEnter the value:");
scanf("%d",a[j]);
}
printf("\nEnter the value of the Matrix B:\n");

for(i=0;i<p;i++)
for(j=0;j<q;j++)
{
printf("\nEnter the value:");
scanf("%d",b[j]);
}

/* MULTIPLICATION */

for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[j]=0;
for(k=0;k<n;k++)
c[j]+=(a[k]*b[k][j]);
}

for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("%d \t",c[j]);
}
}

"
i compiled this code using gcc compiler.... no errors showed up ...
then i executed ...this is what i got ....
"
Enter the size of the first array A:2
2
Enter the size of the second array A:2
2

Enter the value of the Matrix A:

Enter the value:2
Segmentation fault

"
can u guys please tell me what this segementation error is ..?
and y it was thrown up suddenly in my program ,,?
i've never encountered this error before ...

Feb 20 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <11**********************@m58g2000cwm.googlegroups .com>,
John Doe <ba***********@gmail.comwrote:
>int a[10][10],b[10][10],c[10][10];
[...]
>scanf("%d",a[j]);
a is a two-dimensional array, so you want something like &a[i][j].
You have the same problem here:
>scanf("%d",b[j]);
and here:
>c[j]=0;
and here (twice):
>c[j]+=(a[k]*b[k][j]);
and here:
>printf("%d \t",c[j]);
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 20 '07 #2

P: n/a
On Feb 20, 8:51 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <1171985776.676322.268...@m58g2000cwm.googlegroups .com>,

John Doe <bala.mailb...@gmail.comwrote:
int a[10][10],b[10][10],c[10][10];
[...]
scanf("%d",a[j]);

a is a two-dimensional array, so you want something like &a[i][j].
You have the same problem here:
scanf("%d",b[j]);

and here:
c[j]=0;

and here (twice):
c[j]+=(a[k]*b[k][j]);

and here:
printf("%d \t",c[j]);

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
thanks ..
i got the problem solved ...

Feb 20 '07 #3

P: n/a
John Doe said:
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[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
printf("Enter the size of the first array A:");
scanf("%d %d",&m,&n);
Check that the read succeeded. In this case, you are asking scanf to
populate two objects, so it will return 2 if successful.
printf("Enter the size of the second array A:");
scanf("%d %d",&p,&q);
Check that the read succeeded. In this case, you are asking scanf to
populate two objects, so it will return 2 if successful.

Now would be a good point at which to make sure that none of i, j, m,
and n exceed 10. (At first, I thought this was probably causing your
problem, but then I discovered that your test data comprises 2, 2, 2,
2, so let's move on.)
printf("\nEnter the value of the Matrix A:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("\nEnter the value:");
scanf("%d",a[j]);
Two points here. Firstly, a[j] is an array, so it evaluates to &a[j][0],
which is fine (it's the address of an int, which is what's required),
but probably not what you want. It's more likely that you want either
&a[i][j] or perhaps &a[j][i].

Secondly, check that the read succeeded. In this case, you are asking
scanf to populate one object, so it will return 1 if successful.
}
printf("\nEnter the value of the Matrix B:\n");

for(i=0;i<p;i++)
for(j=0;j<q;j++)
{
printf("\nEnter the value:");
scanf("%d",b[j]);
Two points here. Firstly, b[j] is an array, so it evaluates to &b[j][0],
which is fine (it's the address of an int, which is what's required),
but probably not what you want. It's more likely that you want either
&b[i][j] or perhaps &b[j][i].

Secondly, check that the read succeeded. In this case, you are asking
scanf to populate one object, so it will return 1 if successful.
}

/* MULTIPLICATION */

for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[j]=0;
See below for a discussion of the above line.

for(k=0;k<n;k++)
c[j]+=(a[k]*b[k][j]);
If either j or k exceeds 9, you're in trouble. And see below.
}

for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("%d \t",c[j]);
See below.
}
}

"
i compiled this code using gcc compiler.... no errors showed up ...
So did I, and I got:

foo.c:3: warning: return-type defaults to `int'
foo.c:3: warning: function declaration isn't a prototype
foo.c: In function `main':
foo.c:34: incompatible types in assignment
foo.c:36: invalid operands to binary *
foo.c:43: warning: int format, pointer arg (arg 2)
foo.c:45: warning: control reaches end of non-void function
make: *** [foo.o] Error 1

The first couple are no big deal, although it would be good to address
them:

int main(void)

In my (indent-fixed) version of the source, line 34 is:

c[j] = 0;

And this may well be your blow-up. Here, you're trying to write an int
value to an object that is an array of 10 ints. You can't assign to
arrays in C, only to their members.

In each case, I fixed c[j] to read c[i][j]. The "invalid operands to
binary *" error was caused by a[k], so I fixed it to read a[j][k]
(check the logic in case that's not what you meant).

The program then runs to completion, but with very strange results. If I
were you, I would define your arrays like this:

int a[10][10] = {0};
int b[10][10] = {0};
int c[10][10] = {0};

This will ensure that they start off with all member values equal to 0.
With that correction, and a puts("") at the end, the output was
reasonable.
"
can u guys please tell me what this segementation error is ..?
It normally means you (in the general sense!) have done something stupid
with arrays or pointers.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 20 '07 #4

P: n/a
In article <11**********************@m58g2000cwm.googlegroups .com"John Doe" <ba***********@gmail.comwrites:
....
int a[10][10],b[10][10],c[10][10];
....
printf("\nEnter the value of the Matrix A:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("\nEnter the value:");
scanf("%d",a[j]);
This is a strange way to initialise a 2-dimensional array... (Where is
'i' used?) More of this kind of errors do occur.
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[j]=0;
for(k=0;k<n;k++)
c[j]+=(a[k]*b[k][j]);
}
This is also not understandable. Where is 'i' used?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Feb 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.