Segmentation Fault in Simple C Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • parkskier
    New Member
    • Jan 2008
    • 5

    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:
    [CODE=c]void swap (float *a, float *b);

    int main ( )
    {

    float diam[20], a, b;
    int swap_count=0, n;

    FILE *fptr1, *fptr2;
    fptr1=fopen("a1 0.dat", "r");
    fptr2=fopen("a1 2.txt", "w");

    fscanf(fptr1,"% f", &diam[n]);

    do
    {
    for(n=0; n<=19; n++)
    {
    diam[n]=a;
    diam[n+1]=b;
    if(a>b)
    {
    swap_count++;
    void swap (float a, float b);
    }
    else
    {
    a=a;
    b=b;
    swap_count=0;
    }
    }
    }while(swap_cou nt>=1);

    printf("Diamete r=%f\n", diam[n]);


    fclose (fptr1);
    fclose (fptr2);
    }

    void swap (float *a, float *b)
    {
    float temp;

    temp=*a; *a=*b; *b=temp;

    }[/CODE]
    Last edited by Ganon11; Jan 31 '08, 12:22 AM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      It's right here:
      Originally posted by parkskier
      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.

      Comment

      • parkskier
        New Member
        • Jan 2008
        • 5

        #4
        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.

        Comment

        • kalegs
          New Member
          • Jan 2008
          • 1

          #5
          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)

          Comment

          • saikatkolkata
            New Member
            • Jan 2008
            • 17

            #6
            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);

            Comment

            • parkskier
              New Member
              • Jan 2008
              • 5

              #7
              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?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by parkskier
                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.
                Originally posted by parkskier
                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??

                Comment

                • Arulmurugan
                  New Member
                  • Jan 2008
                  • 90

                  #9
                  Originally posted by parkskier
                  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

                  Comment

                  Working...