2010-05-27 55 views
2
#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; 
} 

当我运行此代码时,我不会收到任何错误。除了少数警告,但是当我在另一个系统中运行它时,代码崩溃。我可以知道为什么吗?这是C代码中的错误,以及如何摆脱警告?

+0

错误在第82行;)(你能指出行吗,我懒得数你代码中的行)。 – 2010-05-27 11:22:36

回答

2
fsc1.c: In function ‘ReadFile’: 
fsc1.c:19: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast 

你应该通过&array[count]作为第一个参数,而不是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 

我会叫CF为(*cf)(&array[j], &array[j+1]),没有必要担心元素大小,编译器会照顾它。

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

您需要#include <stdlib.h>

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

FP1应该被声明为FILE *

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

你的阵列是char阵列,而冒泡的第一个参数的预期int *。我会改变Bubblesort采取char *

1

Fopen返回指针。

替换此:

FILE fp1; 

FILE *fp1; 

在主要的开始。

0

strcpy对指针进行操作,但array[count]是一个字符。你还没有提供足够的信息来说明那里应该发生什么。

cf采用指针char但(在BubbleSort)你是一个指针传递它int;但在main中,您将char的数组传递到BubbleSort。也许你应该改变BubbleSort采取一系列char

您还没有包括<stdlib.h>

+0

基本上我正在检查数字超过一个数字..所以,除非我得到一个空格,我把所有的数字放入数组d,然后将其作为字符串复制到c .. – Hick 2010-05-27 11:29:02

0

线

strcpy(array[count],d); 

试图将一个字符串复制到一个字符。你可能想这样做(这取决于它是什么,你想复制):

array[count] = d[count]; 

我认为你需要包括stdlib.h中。