Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem compiling K&R example

Tags:

c

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) {
    /* ... */
}

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

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

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

like image 831
Ree Avatar asked Mar 05 '09 22:03

Ree


People also ask

How do I fix compile-time error?

If the brackets don't all match up, the result is a compile time error. The fix to this compile error is to add a leading round bracket after the println to make the error go away: int x = 10; System.

What causes compile-time error?

A compile time error is an error that is detected by the compiler. Common causes for compile time errors include: Syntax errors such as missing semi-colon or use of a reserved keyword (such as 'class'). When you try and access a variable that is not in scope.

Why can't I compile my Java program?

It means that javac.exe executable file, which exists in bin directory of JDK installation folder is not added to PATH environment variable. You need to add JAVA_HOME/bin folder in your machine's PATH to solve this error. You cannot compile and run Java program until your add Java into your system's PATH variable.

What is compile-time error with example?

Compile-time errors occur when there are syntactical issues present in application code, for example, missing semicolons or parentheses, misspelled keywords or usage of undeclared variables.


2 Answers

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*);
like image 165
Johannes Schaub - litb Avatar answered Sep 28 '22 03:09

Johannes Schaub - litb


That's a common problem :)

The following line tells qsort to expect a pointer to a function with two void* parameters. Unfortunately, strcmp takes two non-modifiable strings hence it's signature is

int (*comp)(const char*, const char*)

instead of what you have:

int (*comp)(void *, void *)

Change the signature of both qsort1 and numeric:

qsort1(void *v[], int left, int right, int (*comp)(const void *, const void *))

and:

int numcmp(const char*, const char*)
like image 39
dirkgently Avatar answered Sep 28 '22 01:09

dirkgently