0

I try to implement the insertion algorithm by calling the insort function. Somehow, along the way I made some mistakes that the terminal print out segmentation faults. Please help. Thanks!

int 
main(int argv,char *argc[]){
    int A[argv-2];
    for(int i=1;i<argv;i++){
        A[i-1]=atoi(*(argc+i));
    }
    insort(&A,argv-1,0);
    for(int i=0;i<argv-1;i++){
        printf("%d",A[i]);
    }
    printf("\n");
    return 0;
}

int
insort(int *A[],int size,int n){
    int temp;
    if(n<size){
        for(int i=n;i>=0 && *(A+i)>*(A+i-1);i--){
            temp=*(A+i-1);
            *(A+i-1)=*(A+i);
            *(A+i)=temp;
        }
    }
    return insort(A,size,n++);
} 
2
  • 3
    Oops! Somehow, when trying to run this code through a debugger, you accidentally posted it on Stack Overflow instead! A debugger will tell you exactly which line has the problem, which will put you on your way to fixing it. If you don't understand the debugger's output, then you can come back and ask a specific question about that. Commented May 28, 2016 at 12:58
  • 1
    Never swap the names argc and argv, it makes the program unreadable. Commented May 28, 2016 at 13:08

1 Answer 1

3

When compiling any program, pay attention to the warnings that the compiler emits.

For each of the warnings, you have to understand why it is given and how to fix it properly.

$ gcc -std=c99 insort.c
insort.c:7:9: warning: implicit declaration of function 'atoi' [-Wimplicit-function-declaration]
insort.c:11:9: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]

To fix these, insert the following lines at the very top:

#include <stdio.h> // for printf
#include <stdlib.h> // for atoi

The next one:

insort.c:10:5: warning: implicit declaration of function 'insort' [-Wimplicit-function-declaration]

To fix it, move the whole insort function above the main function.

The next one:

insort.c:23:17: warning: assignment makes integer from pointer without a cast

This one is really bad. The parameter int *A[] really means int **A, which is a pointer to a pointer to int. To fix this, remove the square brackets.

The next one:

insort.c:22:12: warning: passing argument 1 of 'insort' from incompatible pointer type

The & operator is not necessary. When you pass an array to a function, it decays into a pointer to the beginning of the array. Remove the &.

Now, the program compiles without giving any warnings. That’s good. Next level:

$ gcc -std=c99 -Wall -Wextra -Os insort.c

Wow. Even with all these warnings enabled, the compiler doesn’t complain anymore. That’s good.

(The -Os optimization option is necessary to enable some of the warnings, since they are only tested for when the compiler optimizes the code.)

Now the program compiles. When running it, it seems to be stuck in an endless loop. But the worst errors are fixed now.

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

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.