2010-05-27 4 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 행에 있습니다.) (줄을 나타낼 수 있습니까, 제발 코드에서 줄 수를 셀 수 없습니다.) –

답변

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)(&array[j], &array[j+1]) 등의 CF를 부를 것이다, 필요 요소 크기에 대해 걱정할 필요가 없습니다. #include <stdlib.h>

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

FP1은 FILE *로 선언되어야한다에

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

당신이 필요합니다. 거품 정렬의 최초의 파라미터가 예상 int * 반면

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

어레이는 char 배열이다. Bubblesort를 char *으로 바꿀 것입니다.

1

Fopen이 포인터를 반환합니다.

이 교체 :

FILE fp1; 

주요 시작에서

FILE *fp1; 

와 함께.

0

strcpy은 포인터에서 작동하지만 array[count]은 char입니다. 거기에서 일어날 일을 알려주는 충분한 정보를 제공하지 못했습니다.

cfchar (= BubbleSort)의 포인터를 사용하여 int에 대한 포인터를 전달합니다. 아직 mainchar의 배열을 BubbleSort에 전달합니다. 아마도 BubbleSortchar의 배열로 변경해야합니까?

<stdlib.h>을 포함하지 않았습니다.

strcpy(array[count],d); 

+0

기본적으로 하나 이상의 숫자를 확인하고 있습니다. 그래서 공백 문자가 없으면 모든 숫자를 배열에 넣은 다음 C로 문자열로 복사합니다. – Hick

0

라인은 문자로 문자열을 복사하기 위해 노력하고있다. (당신이 복사하려고하는 것이 무엇인지에 따라) 당신은 아마 그런 짓을 할 :

array[count] = d[count]; 

난 당신이 인 stdlib.h 포함 할 필요가 있다고 생각합니다.

관련 문제