Question

I wanted to make an array of strings and sort them in order based on their length (smallest -> biggest) but the program crashes after all inputs.
And also it bypasses Element 0 (starts directly from element 1 during input)

 #include <stdio.h>
    #include <stdlib.h>
    main()
    {
        int i,j,N;
        printf("\nInput amount of  alphanumericals: ");
        scanf("%d",&N);
            {
                int min;
                char *swap=(char*)malloc(sizeof(char)*150);
                char *A[N],**temp; 
                for(i=0;i<N;i++)
                    *(A+i)=malloc(sizeof(char)*N);//Error Here
                temp=A;
                for(i=0;i<N;i++){
                    printf("\nInput  %d element:",i+1); 
                    fgets(temp+i,150,stdin);//And Here
                }


    printf("\n\nData [");
    for(i=0;i<N;i++)
        printf(" %s",A[i]);
    printf(" ]\n\n");

    //insertion sort
    for(i=0;i<N;i++){
        min=i;
        for(j=i+1;j<N;j++){

            if(strcmp(A[j],A[min])<0){
                min=j;
            }

        }   
        if(min!=i){
            swap=A[i];
            A[i]=A[min];
            A[min]=swap;
        }   
    }
    free(swap);
    printf("\n\nInsertion Sorted Data [");
    for(i=0;i<N;i++)
        printf(" %s",A[i]);
    printf(" ]");
    }
return 0;
}
Was it helpful?

Solution

This error :

 for(i=0;i<N;i++)
    *(A+i)=malloc(sizeof(char)*N);//Error Here

i reproduced with g++ compiler, if this is your case also i.e. you also compiling with g++ add casting to char* :

*(A+i)= (char*) malloc(sizeof(char)*N);//Error Here

The second issue is because you trying to pass char** instead of char* fix :

fgets(*(temp+i),150,stdin);//And Here

Also brackets after scanf("%d",&N); limits scope of variables min and swap , so later should not be visible at all.

OTHER TIPS

First, what happens with element 0: after you enter the number of elements which is read by scanf("%d"), you press Enter so that your input reaches the program. But the scanf calls only reads the number, not the newline character. So the first call to fgets gets what remains of the line — an empty line.

See Get scanf to quit when it reads a newline? for several approaches to solve this problem. The most robust way is to call fgets to read a line (which is expected to call the number), then call sscanf to parse the number in the string in memory.

The second problem is in the call fgets(temp+i,150,stdin). A good compiler would produce a warning like this:

a.c:17:9: warning: passing argument 1 of ‘fgets’ from incompatible pointer type [enabled by default]

You're passing a pointer to a pointer to char (the address of a location containing the address of a string buffer), but fgets expects a pointer to char (the address of a string buffer). Since temp+i is a pointer to the entry in the array A (which is an array of pointers), what you need to pass to fgets is the value that this pointer points to: *(temp+i), which is more commonly written temp[i]. Given what you do with the variable temp, just drop it and use A.

fgets(A[i], 150, stdin);

After that, your sort routing doesn't always return the correct result, but that's another story.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top