Multiple errors on compiling quicksort program's code file in C, on Cygwin.
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Multiple errors on compiling quicksort program's code file in C, on Cygwin.
Have the Quicksort program, in which had to implement only the main, cmp(comparator), & selectPivotIndex functions.
Rest program functions are copied from the book: Algorithms in a nutshell, by George T. Heineman.
Have built the following code, and compiled using the :
Though, once the errors subside, will use the option:
Code:
cc -pedantiC -W -Wall -Werror quicksort.c -o quicksort
Code:
#include<stdio.h>
int partition(void **, int, int *, int, int , int);
int cmp(const void *, const void*);
void do_qsort(void **, int *, int, int);
int selectPivotIndex(void **, int int);
int main(){
void ** vals;//array of pointers
int total_elements;
return do_qsort(vals, total_elements-1, int(* cmp)(const void *, const void*));
}
void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
int pivotIndex;
if (right<= left) return;
pivotIndex = selectPivotIndex(ar, left, right);
pivotIndex = partition(ar, cmp, left, right, pivotIndex);
do_qsort(ar, cmp, left, pivotIndex-1); do_qsort(ar, cmp, pivotInex+!, right);
}
int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int PivotIndex){
int idx, store; void *pivot = ar[pivotIndex]; void *tmp = ar[right];
ar[right] = ar[pivotIndex]; ar[pivotIndex]= tmp;
store = left;
for(idx= left; idx<right;idx++){
if(cmp(ar[idx], pivot <=0){
tmp = ar[idx]; ar[idx]=ar[store]; ar[store]=tmp; store++;
}
}
tmp= ar[right]; ar[right] = ar[store]; ar[store] = tmp;
return store;
}
int cmp(const void * a, const void *b)
{
if ((int*)a<=(int*)b) return 0;
else return 1;
}
int selectPivotIndex(void ** ar, int left, int right)
{
int median;
median = (left+right)/2;
if(median %2==0) median = median +1;
return median;
}
Have got many errors, as shown below:
Code:
quicksort.c: In function ‘main’:
quicksort.c:10:49: error: expected expression before ‘int’
10 | return do_qsort(vals, total_elements-1, int(* cmp)(const void *, const void*));
| ^~~
quicksort.c:10:45: warning: passing argument 2 of ‘do_qsort’ makes pointer from integer without a cast [-Wint-conversion]
10 | return do_qsort(vals, total_elements-1, int(* cmp)(const void *, const void*));
| ~~~~~~~~~~~~~~^~
| |
| int
quicksort.c:4:24: note: expected ‘int *’ but argument is of type ‘int’
4 | void do_qsort(void **, int *, int, int);
| ^~~~~
quicksort.c:10:16: error: too few arguments to function ‘do_qsort’
10 | return do_qsort(vals, total_elements-1, int(* cmp)(const void *, const void*));
| ^~~~~~~~
quicksort.c:4:6: note: declared here
4 | void do_qsort(void **, int *, int, int);
| ^~~~~~~~
quicksort.c: At top level:
quicksort.c:13:90: error: redefinition of parameter ‘right’
13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
| ~~~~^~~~~
quicksort.c:13:30: note: previous definition of ‘right’ with type ‘int’
13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
| ~~~~^~~~~
quicksort.c:13:6: error: conflicting types for ‘do_qsort’; have ‘void(void **, int, int (*)(const void *, const void *), int, int)’
13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
| ^~~~~~~~
quicksort.c:4:6: note: previous declaration of ‘do_qsort’ with type ‘void(void **, int *, int, int)’
4 | void do_qsort(void **, int *, int, int);
| ^~~~~~~~
quicksort.c: In function ‘do_qsort’:
quicksort.c:17:36: warning: passing argument 2 of ‘partition’ makes integer from pointer without a cast [-Wint-conversion]
17 | pivotIndex = partition(ar, cmp, left, right, pivotIndex);
| ^~~
| |
| int (*)(const void *, const void *)
quicksort.c:2:24: note: expected ‘int’ but argument is of type ‘int (*)(const void *, const void *)’
2 | int partition(void **, int, int *, int, int , int);
| ^~~
quicksort.c:17:41: warning: passing argument 3 of ‘partition’ makes pointer from integer without a cast [-Wint-conversion]
17 | pivotIndex = partition(ar, cmp, left, right, pivotIndex);
| ^~~~
| |
| int
quicksort.c:2:29: note: expected ‘int *’ but argument is of type ‘int’
2 | int partition(void **, int, int *, int, int , int);
| ^~~~~
quicksort.c:17:22: error: too few arguments to function ‘partition’
17 | pivotIndex = partition(ar, cmp, left, right, pivotIndex);
| ^~~~~~~~~
quicksort.c:2:5: note: declared here
2 | int partition(void **, int, int *, int, int , int);
| ^~~~~~~~~
quicksort.c:18:22: warning: passing argument 2 of ‘do_qsort’ makes integer from pointer without a cast [-Wint-conversion]
18 | do_qsort(ar, cmp, left, pivotIndex-1); do_qsort(ar, cmp, pivotInex+!, right);
| ^~~
| |
| int (*)(const void *, const void *)
quicksort.c:13:30: note: expected ‘int’ but argument is of type ‘int (*)(const void *, const void *)’
13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
| ~~~~^~~~~
quicksort.c:18:27: warning: passing argument 3 of ‘do_qsort’ makes pointer from integer without a cast [-Wint-conversion]
18 | do_qsort(ar, cmp, left, pivotIndex-1); do_qsort(ar, cmp, pivotInex+!, right);
| ^~~~
| |
| int
quicksort.c:13:42: note: expected ‘int (*)(const void *, const void *)’ but argument is of type ‘int’
13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
quicksort.c:18:9: error: too few arguments to function ‘do_qsort’
18 | do_qsort(ar, cmp, left, pivotIndex-1); do_qsort(ar, cmp, pivotInex+!, right);
| ^~~~~~~~
quicksort.c:13:6: note: declared here
13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
| ^~~~~~~~
quicksort.c:18:66: error: ‘pivotInex’ undeclared (first use in this function); did you mean ‘pivotIndex’?
18 | do_qsort(ar, cmp, left, pivotIndex-1); do_qsort(ar, cmp, pivotInex+!, right);
| ^~~~~~~~~
| pivotIndex
quicksort.c:18:66: note: each undeclared identifier is reported only once for each function it appears in
quicksort.c:18:77: error: expected expression before ‘,’ token
18 | do_qsort(ar, cmp, left, pivotIndex-1); do_qsort(ar, cmp, pivotInex+!, right);
| ^
quicksort.c:18:61: warning: passing argument 2 of ‘do_qsort’ makes integer from pointer without a cast [-Wint-conversion]
18 | do_qsort(ar, cmp, left, pivotIndex-1); do_qsort(ar, cmp, pivotInex+!, right);
| ^~~
| |
| int (*)(const void *, const void *)
quicksort.c:13:30: note: expected ‘int’ but argument is of type ‘int (*)(const void *, const void *)’
13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
| ~~~~^~~~~
quicksort.c:18:48: error: too few arguments to function ‘do_qsort’
18 | do_qsort(ar, cmp, left, pivotIndex-1); do_qsort(ar, cmp, pivotInex+!, right);
| ^~~~~~~~
quicksort.c:13:6: note: declared here
13 | void do_qsort(void **ar, int right, int(*cmp)(const void *, const void *), int left, int right){
| ^~~~~~~~
quicksort.c: At top level:
quicksort.c:21:5: error: conflicting types for ‘partition’; have ‘int(void **, int (*)(const void *, const void *), int, int, int)’
21 | int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int PivotIndex){
| ^~~~~~~~~
quicksort.c:2:5: note: previous declaration of ‘partition’ with type ‘int(void **, int, int *, int, int, int)’
2 | int partition(void **, int, int *, int, int , int);
| ^~~~~~~~~
quicksort.c: In function ‘partition’:
quicksort.c:22:41: error: ‘pivotIndex’ undeclared (first use in this function); did you mean ‘PivotIndex’?
22 | int idx, store; void *pivot = ar[pivotIndex]; void *tmp = ar[right];
| ^~~~~~~~~~
| PivotIndex
quicksort.c:26:38: warning: passing argument 2 of ‘cmp’ makes pointer from integer without a cast [-Wint-conversion]
26 | if(cmp(ar[idx], pivot <=0){
| ~~~~~~^~~
| |
| int
quicksort.c:26:38: note: expected ‘const void *’ but argument is of type ‘int’
quicksort.c:26:42: error: expected ‘)’ before ‘{’ token
26 | if(cmp(ar[idx], pivot <=0){
| ~ ^
| )
quicksort.c:29:8: error: expected expression before ‘}’ token
29 | }
| ^
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.