473,216 Members | 1,238 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,216 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 5795
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...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.