问题编译K&安培; R为例 [英] Problem compiling K&R example

查看:91
本文介绍了问题编译K&安培; R为例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在编写本书的第5.11节psented示例程序$ P $。我已删除大部分的code,并且只留下相关的东西。

I'm having trouble compiling the example program presented in section 5.11 of the book. I have removed most of the code and left only the relevant stuff.

#define MAXLINES 5000
char *lineptr[MAXLINES];

void qsort1(void *lineptr[], int left, int right, int (*comp)(void *, void *));
int numcmp(char *, char *);

main(int argc, char *argv[]) {
    int numeric = 1;
    /* ... */
    qsort1((void**) lineptr, 0, 100, (int (*)(void*, void*))(numeric ? numcmp : strcmp));
}

void qsort1(void *v[], int left, int right, int (*comp)(void *, void *)) {
    /* ... */
}

int numcmp(char *s1, char *s2) {
    /* ... */
}

问题是,code不编译(我使用的是数字火星编译器)。我得到的错误是这样的:

The problem is that the code doesn't compile (I'm using Digital Mars compiler). The error I get is this:

        qsort1((void**) lineptr, 0, nlines - 1, (int (*)(void*, void*))(numeric
? numcmp : strcmp));

                 ^
go.c(19) : Error: need explicit cast to convert
from: int (*C func)(char const *,char const *)
to  : int (*C func)(char *,char *)
--- errorlevel 1

有一定有什么毛病我虽然正确粘贴了code从书的声明。我不知道足够做出正确的改变(关于函数指针的部分当然可以被更广泛地写的)。

There must be something wrong with the declarations although I pasted the code from the book correctly. I don't know enough to make the right changes (the section about the function pointers could certainly have been written more extensively).

编辑:我应该提到,我在读这本书的ANSI版本

I should have mentioned that I'm reading the ANSI version of the book.

推荐答案

我觉得这个错误来自于一个事实,即旧的C不知道常量尚:STRCMP出现了两个指向非const字符(的char * )我认为(这可能是为什么它编译当年,但与你的编译器)的原因。但是,现在需要的strcmp 字符常量* 为const char * 是同一回事)。你的函数原型改成这样:

I think the error comes from the fact that old C did not know const yet: strcmp there took two pointers to non-const characters (char *) i think (which could be the reason why it compiled back then, but not with your compiler). However, nowadays strcmp takes char const* (const char* is the same thing). Change your function prototype to this:

int numcmp(char const*, char const*);

这篇关于问题编译K&安培; R为例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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