0

I can't find a mistake in my code. When I run it, it says segmentation fault(core dumped). I read topics, but couldn't understand my mistake. It's in appropriate directory. I would be very thankful if some can help me.

#include<stdio.h>
#define MAX 50

int main(){

    int i,n,j,t;
    double a[MAX];

    do{
        printf("enter a lenght of your array");
        scanf("%d", &n);
    }while (n<1 || n>MAX);

    printf("enter the elements of your array");
    for (i=0; i<n; i++) {
        printf("a[%d]= ", i);
        scanf("%lf", &a[i]);
    }

    for(i=0;i<n/2;i++){
        for(j=n-1;j>n/2;j++){
            t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
    }

    for(i=0; i<n; i++) {
        printf("%lf", a[i]);
    }
    return 0;
}
3
  • 2
    Run in a debugger to catch the crash, and see where in your code it happens. Then examine the values of variables to try and figure out why it happens. Commented Nov 10, 2015 at 8:57
  • You should check that the indexes used (i, and n) are not out of bounds (0<=i<MAX). Commented Nov 10, 2015 at 8:58
  • Yes, using a debugger would let you know that the failing line is a[i]=a[j];. Still using this debugger (or just add a printf of i and j values) would have shown you that j is always increasing. At at a moment a[j] crashes. Commented Nov 10, 2015 at 8:59

2 Answers 2

4
  for(i=0;i<n/2;i++){
      for(j=n-1;j>n/2;j++){     /*    <--- This loop      */

In this loop you should decrement j , so do j--.

Because j=n-1 and you increment it , and a has max number of elements, so this loop will access index out of bounds and cause undefined behaviour.

So you should decrement j in loop.

Sign up to request clarification or add additional context in comments.

3 Comments

how can i swap first and last, second and penultimate in array ?
@Luka I don't think you need two loops for that . Increment i and decrement j in single loop. Initialize them as i=0 and j=n-1 , and swap the numbers .
@Luka Just this loop will do - for(i=0,j=n-1;(i<n/2)||(j>n/2);i++,j--) and do swapping inside this loop.
0

The problem in your code is in the line

for(j=n-1;j>n/2;j++){

In this line you are incrementing the value of j beyond its allocated limits. And you are trying to access a[j]. This is an example of Out of bound Array access. i.e. you are trying to access that element of the array which you have not declared.

It might be possible that a[j] lies in a memory location that's not owned by your currently running program (which will probably cause your program to crash with something like a segmentation fault). Probably this is the reason for the segmentation fault in your code.

Edit: After running gdb on your program, I found that that the program terminated with signal SIGSEGV

SIGSEGV is the signal sent to a process when it makes an invalid memory reference, or segmentation fault.

I suggest you to also use gdb for debugging your errors

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.