其中在这个C code中的错误,以及如何摆脱这些警告? [英] where is the error in this C code, and how to get rid of the warnings?

查看:271
本文介绍了其中在这个C code中的错误,以及如何摆脱这些警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
//这个程序是一个排序的应用程序,从文件读取的数字序列,并打印在屏幕上。从这里的文件读取,是一个回调函数。的typedef INT(* CompFunc)(为const char *,为const char *);
的typedef INT(* ReadCheck)(CHAR nullcheck);
int数组[100];
//让这个函数库本身完成。它不在乎到哪里比较功能以及它是如何实现的。这意味着假设功能想做排序升序或降序排列则变更必须由客户端code在谁将会实施的lib code中的比较函数来完成。
无效ReadFile的(FILE * FP,ReadCheck RC)
{
    所以char a;
    字符D [100];
    诠释计数= 0,COUNT1 = 0,K;
    A =龟etc(FP);    而(1!=(* RC)(a)条)
    {如果(A =='')
        {        D [COUNT1] ='\\ 0';
        数组[计数] =的atoi(D);        数=计+ 1;        的printf(%S \\ n,D);
        memset的(D,'\\ 0',100);
        的count1 = 0;
        }
        其他
        {        D [COUNT1] = A;
        的count1 = COUNT1 + 1;
        }        A =龟etc(FP);
    }}
无效冒泡(的char *数组,诠释大小,INT elem_size,CompFunc CF)
{INT I,J,K;
    INT *温度;
    对于(i = 0; I<大小;我++)
    {
        为(J = 0; J<大小-1; J ++)
        {
            //使回调到比较函数
            如果(1 ==(* CF)(阵列+ J * elem_size,阵列+(J + 1)* elem_size))
                {    //元素互换
                    TEMP =的malloc(sizeof的(字符*)* elem_size);
                    的memcpy(温度,阵列+ J * elem_size,elem_size);
                    的memcpy(阵列+ J * elem_size,阵列+(J + 1)* elem_size,elem_size);
                    的memcpy(阵列+(J + 1)* elem_size,温度,elem_size);
                    免费(TEMP);
                }
        }
    }
}//让这些功能在客户端完成INT比较(字符* EL1,字符* EL2)
    {
        INT中element1 = *为(int *)EL1;
        INT在element2 = *为(int *)EL2;        如果(中element1<在element2)
            返回-1;
        如果(中element1>在element2)
            返回1;
        返回0;
    }INT ReadChecked(CHAR nullcheck)
    {
        如果(nullcheck =='\\ n')
            返回1;
        其他
            返回0;
    }
诠释的main()
{
    FILE * FP1;
    时int k;
    FP1 = FOPEN(readdata.txt,R);    ReadFile的(FP1,&安培; ReadChecked);
的printf(排序之前\\ n);
    为(K = 0; K&10 6; k ++)
    的printf(%d个\\ N阵列[K]);        冒泡((字符*)阵列,5,的sizeof(数组[0]),放大器,比较);
    的printf(排序\\ n后);
    为(K = 0; K&小于5; k ++)
    的printf(%d个\\ N阵列[K]);返回0;
}

当我运行这个code我没有得到任何错误。除了一些警告,但是当我在另一个系统中,code崩溃运行它。能告诉我为什么?


解决方案

  fsc1.c:在功能的ReadFile:
fsc1.c:19:警告:传递的strcpy的参数1,使指针从整数没有投

您应该通过&放大器;阵列[计数] 作为第一个参数,而不是数组[计数]

  fsc1.c:在函数'冒泡':
fsc1.c:40:警告:从兼容的指针类型通过CF的参数1
fsc1.c:40:警告:从兼容的指针类型通过CF的论点2

我会叫CF为(* CF)(安培;阵列[J],和放大器;阵列[J + 1]),没有必要担心单元尺寸因为编译器会照顾它。

  fsc1.c:43:警告:内建函数'的malloc'不兼容的隐式声明
fsc1.c:47:警告:内建函数不兼容的隐式声明的免费

您需要的#include<&stdlib.h中GT;

  fsc1.c:在函数'主':
fsc1.c:80:错误:不兼容的类型赋值

FP1应该被声明为 FILE *

  fsc1.c:82:警告:从兼容的指针类型传递'冒泡'的参数1

您数组是一个字符阵列,而冒泡的第一个参数预期的为int * 。我会改变冒泡采取的char *

#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?

解决方案

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 *.

这篇关于其中在这个C code中的错误,以及如何摆脱这些警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆