Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 810853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:57:04+00:00 2026-05-15T00:57:04+00:00

#include<stdlib.h> #include<stdio.h> #include<string.h> //This program is a sorting application that reads a sequence of

  • 0
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
//This program is a sorting application that reads a sequence of numbers from a file and prints them on the screen . The reading from the file here , is a call back function . 

typedef int (*CompFunc)(const char* , const char* );
typedef int (*ReadCheck)(char nullcheck);
int array[100];
//Let this function be done in the library itself . It doesn't care as to where the compare function and how is it implemented . Meaning suppose the function wants to do sort in ascending order or in descending order then the changes have to be done by the client code in the "COMPARE" function who will be implementing the lib code . 
void ReadFile(FILE *fp,ReadCheck rc)
{
    char a;
    char d[100];
    int count = 0,count1=0,k;
    a=fgetc(fp);

    while (1 !=(*rc)(a) ) 
    {   if(a==' ')
        {

        d[count1]='\0';
        array[count]=atoi(d);

        count=count+1;

        printf("%s \n",d);
        memset(d,'\0',100);
        count1=0;
        }
        else
        {

        d[count1]=a;
        count1=count1+1;


        }

        a=fgetc(fp);
    }

}
void Bubblesort(char* array , int size , int elem_size , CompFunc cf)
{   int i,j,k;
    int *temp;
    for( i=0;i < size ;i++)
    {
        for ( j=0;j < size -1 ; j++)
        {
            // make the callback to the comparision function
            if(1 == (*cf)(array+j*elem_size,array+ (j+1)*elem_size))
                {

    //interchanging of elements 
                    temp =  malloc(sizeof(char *) * elem_size);
                    memcpy(temp , array+j*elem_size,elem_size);
                    memcpy(array+j*elem_size,array+(j+1)*elem_size,elem_size);
                    memcpy(array + (j+1)*elem_size , temp , elem_size);
                    free(temp);
                }
        }
    }


}



//Let these functions be done at the client side 

int Compare(char* el1 , char* el2)
    {
        int element1 = *(int*)el1;
        int element2 = *(int*)el2;

        if(element1 < element2 )
            return -1;
        if(element1 > element2)
            return 1 ;
        return 0;
    }

int ReadChecked(char nullcheck)
    {
        if (nullcheck=='\n')
            return 1;
        else 
            return 0;
    }
int main()
{
    FILE *fp1;
    int k;
    fp1=fopen("readdata.txt","r");

    ReadFile(fp1,&ReadChecked);
printf("before sorting \n");
    for (k=0;k<6;k++)
    printf("%d \n",array[k]);

        Bubblesort((char*)array,5,sizeof(array[0]),&Compare);
    printf("after sorting \n");
    for (k=0;k<5;k++)
    printf("%d \n",array[k]);

return 0;
}

When I run this code I don’t get any error. Apart from few warnings, but when I run it in another system, the code crashes. May I know why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:57:05+00:00Added an answer on May 15, 2026 at 12:57 am
    fsc1.c: In function ‘ReadFile’:
    fsc1.c:19: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast
    

    You should be passing &array[count] as the first parameter, not array[count].

    fsc1.c: In function ‘Bubblesort’:
    fsc1.c:40: warning: passing argument 1 of ‘cf’ from incompatible pointer type
    fsc1.c:40: warning: passing argument 2 of ‘cf’ from incompatible pointer type
    

    I would call cf as (*cf)(&array[j], &array[j+1]), no need to worry about element size as compiler will take care of it.

    fsc1.c:43: warning: incompatible implicit declaration of built-in function ‘malloc’
    fsc1.c:47: warning: incompatible implicit declaration of built-in function ‘free’
    

    You need to #include <stdlib.h>

    fsc1.c: In function ‘main’:
    fsc1.c:80: error: incompatible types in assignment
    

    fp1 should be declared as a FILE *.

    fsc1.c:82: warning: passing argument 1 of ‘Bubblesort’ from incompatible pointer type
    

    Your array is a char array, whereas the first parameter of Bubblesort expecting an int *. I would change Bubblesort to take a char *.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
This is my socket server with fork: #include <stdio.h> #include <string.h> #include <stdlib.h> #include
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
I'm looking at this program that reads input lines and then sorts them, from
Hey Folks i am trying to compile this C++ program: #include <stdio.h> #include <string.h>
This is the code #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h>
See the following program: #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> main(void){ printf(Array
This is program is input some string from a file, then, push strings into
here is the source code of the program. #include <stdio.h> #include <stdlib.h> #include <string.h>
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.