473,382 Members | 1,204 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 Fault in Simple C Program

Ok, so I got a segmentation fault when I tried to run the program I created. I understand what this error means, but I don't know where it is. If someone can quickly look through my code and point it out that'd be great. BTW, the code isn't completely finished (ie the file to write on isn't being written on yet) but this doesn't effect this segmentation fault as far as I know.

I'm using Linux Red Hat to run the program and Emacs to compile it.

Syntax:
Expand|Select|Wrap|Line Numbers
  1. void swap (float *a, float *b);
  2.  
  3. int main ( )
  4. {
  5.  
  6.   float diam[20], a, b;
  7.   int swap_count=0, n;
  8.  
  9.   FILE *fptr1, *fptr2;
  10.   fptr1=fopen("a10.dat", "r");
  11.   fptr2=fopen("a12.txt", "w");
  12.  
  13.   fscanf(fptr1,"%f", &diam[n]);
  14.  
  15. do
  16.   {
  17.     for(n=0; n<=19; n++)
  18.       {
  19.     diam[n]=a;
  20.     diam[n+1]=b;
  21.     if(a>b)
  22.       {
  23.         swap_count++;
  24.         void swap (float a, float b);
  25.       }
  26.     else
  27.       {
  28.         a=a;
  29.         b=b;
  30.         swap_count=0;
  31.       }
  32.       }
  33.   }while(swap_count>=1);
  34.  
  35.  printf("Diameter=%f\n", diam[n]);
  36.  
  37.  
  38.       fclose (fptr1);
  39.       fclose (fptr2);
  40. }
  41.  
  42. void swap (float *a, float *b)
  43. {
  44.   float temp;
  45.  
  46.   temp=*a; *a=*b; *b=temp;
  47.  
  48. }
Jan 30 '08 #1
8 5808
Ganon11
3,652 Expert 2GB
This may not be causing your segfault, but when using the swap function, you should not say "void swap(float a, float b);" This is creating a new function named swap that takes two floats as arguments. You should instead use "swap(a, b);", which will actually use the swap function you created with your variables a and b.
Jan 31 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
It's right here:
for(n=0; n<=19; n++)
{
diam[n]=a;
diam[n+1]=b;
etc...\
When n is 19, then n+1 is 20. Your array is 20 which means the largest index is 19. That is, diam[20] is outside your array bound.

Hence the seg fault.
Jan 31 '08 #3
So to fix that I should just replace the n<=19 with a different number such as....err...20? I'm not exactly sure how to fix this.

I tried different numbers in place of 19 (such at 18-21) and still get the segmentation fault.
Jan 31 '08 #4
kalegs
1
i am not sure what you are trying to achieve...but i guess
24. void swap (float a, float b); should be swap(&a,&b)
Jan 31 '08 #5
1. u haven't initialized 'a' and 'b' so in the for loop..it should be
a=diam[n];
b=diam[n+1];

2. function swap(&a,&b);
Jan 31 '08 #6
The program is supposed to read in 20 values from a file and put them in an array. Then it is supposed to sort these values from smallest to largest using the swap function to compare individual values and change them.

I tried do what was listed above but to no avail. The error I get when I change void swap (float a, float b) to swap (&a, &b) is this:

error: cannot convert `float*' to `float' for argument `1' to `voidswap(float, float)'

Help?
Jan 31 '08 #7
weaknessforcats
9,208 Expert Mod 8TB
tried different numbers in place of 19 (such at 18-21) and still get the segmentation fault.
Your calculated index value must be in the range 0-19. Those are your 20 array elements.
error: cannot convert `float*' to `float' for argument `1' to `voidswap(float, float)'
I have no idea what that's about. It compiles fine for me as swap(&A,&b).

That error looks like you have swap that has float arguments and not float* arguments. You didn't change that and not tell us, did you??
Jan 31 '08 #8
The program is supposed to read in 20 values from a file and put them in an array. Then it is supposed to sort these values from smallest to largest using the swap function to compare individual values and change them.

I tried do what was listed above but to no avail. The error I get when I change void swap (float a, float b) to swap (&a, &b) is this:

error: cannot convert `float*' to `float' for argument `1' to `voidswap(float, float)'

Help?
Hi,
the program will not suppose to do, what you expected, i am seeing lot of mistake here.
1)
fscanf(fptr1,"%f", &diam[n]);
what will happen ,when "n" is a junk val, because not inited.

2) what are you tring to achive
diam[n]=a;
diam[n+1]=b;
3) Wrong statement
void swap (float a, float b);
Different compiler may say different error, but it is not valied atleast here.
4) what are you doing here..
a=a;
b=b;
i hope now you know , what is you requiremnet , so write write your own code.

Regards
Arul
Feb 1 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
10
by: Peter Dragun | last post by:
I am generally new to programming under Unix, but know how to code under Windows. I have to create a simple program, that takes the following information from a file using redirection: 4 4 4 4 4...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
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...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
3
by: jr.freester | last post by:
I have created to classes Matrix and System. System is made up of type matrix. ---------------------------------------------------------------------------------- class Matrix { private: int...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.