468,491 Members | 1,950 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

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 5484
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,207 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,207 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

Post your reply

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

Similar topics

2 posts views Thread by sivignon | last post: by
3 posts views Thread by diyanat | last post: by
10 posts views Thread by Peter Dragun | last post: by
16 posts views Thread by laberth | last post: by
5 posts views Thread by Fra-it | last post: by
7 posts views Thread by pycraze | last post: by
6 posts views Thread by DanielJohnson | last post: by
3 posts views Thread by jr.freester | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.