473,382 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

segmentation error ....!

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
4 1928
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Justin Tang | last post by:
Hi I am wondering about the segmentation fault in PHP. Namely, I'm running PHP version 4.6.9 on my server right now and when I try to process a large piece of text via textarea(3k+), the resulting...
3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
21
by: user | last post by:
I just finish writing LAB2 with no errors when compiling, but once i run it, i get "segmentation fault".. i don't know what is wrong, can anyoen tell me what it means and how i can fix it? thx!
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
2
by: zl2k | last post by:
hi, all I am using a 2 dimensioanl array implemented by vector<vector<long> >. When the row number grows to 8 and I am trying to insert a new row, I got the segmentation error. However, if I...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
3
by: Dhieraj | last post by:
While compiling a C++ code I am getting the following error : CC -c -I/opt/iona/artix/2.0/include -I/opt/iona/asp/6.0/include -I/opt/ar/api63/include -I//var/tmp/vidya/aotscommon/include ...
0
by: ollii | last post by:
Hello evryboody, i created client and srever program that they can both communicate together by TCP and UDP, but when i want to send message to server from client i get error on the server i get...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
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,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.