Why when we use qsort(), int(*compar)(const void*,const void*) haven't add any paraments in, but can also do their functions?
Such as this :
double vals[NUM];
qsort(vals,NUM,sizeof(double),mycomp);
When you append parenthesis to the identifier representing a function, with a list of parameters between, you're calling the function.
In this case, you don't want to call the function, however. You want qsort to call the function to determine whether or not one element is larger or smaller than the other.
It seems you are asking about function pointers in general.
Here is a simple example of function pointer:
int compare(int i, int j)
{
return i > j ? i : j;
}
void process(int i, int j, int(*pfunc)(int, int))
{
printf("%d\n", pfunc(i, j));
}
int main(void)
{
process(1, 2, compare);
return 0;
}
In this example process is sort of like qsort, while compare is the function which we define ourselves. We need to tell qsort how to compare data, but we can't access qsort directly, so pass our own function to it which tells qsort how to compare.
qsort is generic. It doesn't know what type of data it is processing, so it can't simply compare elements using <. What it does instead is let you, the programmer, give it a function to use to compare elements. So, yes you do care about how mycomp works, you need to define it.
int mycomp(const void *a, const void *b) {
const double *lhs = a;
const double *rhs = b;
if (*lhs < *rhs) return -1;
if (*lhs > *rhs) return 1;
return 0;
}
inside of qsort, every time it needs to compare two elements it will pass them to your mycomp function and examine the result. If you define it wrong, your array will be incorrectly arranged (unsorted).
Related
I have the following function that accepts a varying number of integer parameters and returns the sum.
int sum(int a, ...){
va_list nums;
va_start(nums, a);
int res=0;
for(int i=0; i<a; i++) {
res += va_arg(nums, int);
}
va_end(nums);
return res;
}
I need to pass each value of the array as a parameter to the function rather than passing the array itself. The array can be of varying length leading to a varying length of arguments to pass too.
printf("The sum is: %d", sum(size, args[0], args[1], ```need all elements of args[] array here```));
To put forth some perspective, I'm using this sum function to understand how I can go about doing this. It would be helpful to know how to achieve this in a more general setting rather than this exact function.
Please let me know if you need any more information.
Please do look at this question, which is similar, however, I require a solution in C.
The short answer is that there's no way to do exactly this in the C language. There is no ES6-like spread operator in C, nor similar functionality. I don't think there's any particular reason why they couldn't (you would just have to push more arguments onto the stack); they just never made one.
However, there are various other things you can do:
If variadic arguments were already passed into the function calling your function, you can pass along the va_list to a function declared to take a va_list. See Passing variable arguments to another function that accepts a variable argument list
As #JonathanLeffer suggests, the most natural way to write this code in C is by constructing an array of what "would be" your variadic arguments, and passing that into a function that expects an array (well, technically, a pointer, because arrays decay to pointers). For example:
int sum_array(int a, int nums[]){
int res=0;
for(int i=0; i<a; i++) {
res += nums[i];
}
return res;
}
In certain circumstances, it may be more convenient for a function like sum_array to take only the nums array/pointer, which would itself indicate the end of the array with a 0 or -1 value in the last slot. This is just another convention for indicating the end, which the caller has to set up.
You could then, if you really wanted to, write a variadic function that collects its arguments into an array and calls sum_array, if you want a variadic version as well. (Of course, you could also just implement the variadic and array versions separately, but for nontrivial functions it may be a pain to implement them twice.)
int sum_variadic(int a, ...){
va_list nums;
va_start(nums, a);
int arr[a];
for(int i=0; i<a; i++) {
arr[i] = va_arg(nums, int);
}
va_end(nums);
return sum_array(a, arr);
}
you could also use a variadic macro for the same purpose:
#define sum_macro(size, ...) sum_array(size, (int[]){__VA_ARGS__})
In summary: going from variadic to array in C is trivial, going from array to variadic is impossible.
You can also use extensions to the C language to do it anyway, as described in Passing a dynamic set of variadic arguments to a C function and In C, given a variable list of arguments, how to build a function call using them?, which #KamilCuk linked to.
You can avoid passing the number of arguments explicitly by using a variadic macro that constructs a compound literal array instead of a vararg function:
#include <stdio.h>
#define sum_int(...) (sum_int)(sizeof((int[]){__VA_ARGS__}) / sizeof(int), (int[]){__VA_ARGS__})
int (sum_int)(size_t count, const int *a) {
int sum = 0;
for (size_t i = 0; i < count; i++) {
sum += a[i];
}
return sum;
}
int main() {
printf("sum = %d\n", sum_int(1, 2, 3));
return 0;
}
This approach can be used for any purpose, as long as the types of the variable arguments are converted implicitly to the array type.
If you want to pass just the arguments to your sum function without changing its definition, you can use a variation of the macro:
#include <stdio.h>
int sum(int a, ...) {
va_list nums;
va_start(nums, a);
int res = 0;
for (int i = 0; i < a; i++) {
res += va_arg(nums, int);
}
va_end(nums);
return res;
}
#define sum(...) (sum)((int)(sizeof((int[]){__VA_ARGS__}) / sizeof(int)), __VA_ARGS__)
int main() {
printf("sum = %d\n", sum(1, 2, 3));
return 0;
}
Note however that there is no type checking with this approach and sum(1, 2.0, 3) would have undefined behavior.
I was writing some code and I realised that I kept on passing arguments to functions when the function didn't use the argument. Only the sub-function with the function( or the sub-sub-function within the sub-function and so on...) used it.
The actual function never used the arguments at all, it's only purpose was to "relay" the arguments to the sub-function(or the sub-sub-function and so on)
Eg:
int search(int (*board)[DIM],int search_digit,int * bestRow,int *bestCol)
{
//some code,haven't use arguments bestRow,bestCol
longest=seq_length( board,longest,search_digit,bestRow,bestCol,r,c); //sub-function
//some code,haven't use arguments bestRow,bestCol
}
int seq_length(int board[][DIM],int longest,int search_digit,int * bestRow,int *bestCol,int row,int col)
{
//some code,haven't use arguments bestRow,bestCol
longest=updateLongest_best(bestRow,bestCol,longest,seqLength,row,col); //sub-sub-function
//some code,haven't use arguments bestRow,bestCol
}
int updateLongest_best(int* bestRow,int *bestCol,int longest,int seqLength,int row,int col)
{
//finally used arguments bestRow,bestCol
}
Is there an elegant way to circumvent the passing of redunant arguments? Or is this just inherent part of C?
Converting my comment into an answer.
The 'obvious' alternative is to 'pass' those variables via a global variable. This is not (repeat, NOT) a good idea — the code as written is better.
If you have two or more arguments that are relayed from one function to another (as here), you could consider combining them into a structure that is passed by pointer so there's only one direct argument to each called function.
Note that if the functions that are called from a sub-function need the information passed to the calling function by the user, then the arguments are not 'redundant'; they're necessary, even if verbose.
In outline:
typedef struct BestInfo
{
int row;
int col;
} BestInfo;
int search(int (*board)[DIM], int search_digit, BestInfo *best)
{
// some code; doesn't use argument best
longest=seq_length(board, longest, search_digit, best, r, c); //sub-function
// some more code; doesn't use argument best
}
int seq_length(int board[][DIM], int longest, int search_digit, BestInfo *best, int row, int col)
{
// code that doesn't use argument best
longest = updateLongest_best(best, longest, seqLength, row, col); //sub-sub-function
// more code that doesn't use argument best
return …;
}
int updateLongest_best(BestInfo *best, int longest, int seqLength, int row, int col)
{
// Finally use argument best: best->row, best->col
// You can split the structure when appropriate
int r1 = one_more_function(&best->row);
int r2 = another_function(&best->col);
return computation_using(r1, r2);
}
is there any way to use static array, defined in main(), in another function, without giving it to the function as a parameter?
For example:
main() has defined array:
int Array[10];
filled with integers. I'd like to create a comparing function for qsort, that has to have this header:
int compar (const void* a, const void* b);
and I would like it to decide like this:
if Array[a]<Array[b] return 1
etc...
This array cannot be given to qsort directly, but is required for exact sorting.
Also, this array has to be static (no reallocing).
Does anyone have any ideas?
The only way is, of course, so make the address of the array available as a global variable.
This is possible even if the array itself is inside main(), but you have to initialize the global to the properly scoped address, and watch the life-time, of course.
int *mains_array;
static int qsort_callback(const void *a, const void *b)
{
/* use mains_array */
}
int main(void)
{
int secret_array[100];
mains_array = secret_array;
qsort(something, something, qsort_callback);
}
It's a pretty ugly solution, it should be given more thought.
You can't access a local variable from another function. You need to make it global or file scoped.
Global:
/* Declare Array outside any function */
int Array[10];
int main(...
or, file scoped:
/* Declare Array outside any function */
static int Array[10];
int main(...
Note:
Your compare function will receive pointers to the elements to compare. If you are sorting an array of int you need to dereference the pointer in your compare function:
I'm assuming that Array isn't the array you want to sort, but an array that contains information on how to sort an array.
int compare (const void * ap, const void * bp)
{
int a = *((int*)ap);
int b = *((int*)bp);
if (Array[a] < Array[b]) {
return 1;
}
if (Array[a] > Array[b]) {
return -1;
}
return 0;
}
qsort requires the address of the array, so you don't have a choice. But where is defined the array, that does not matter. You just need to be able to refer to it.
The qsort signature is:
void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
So you will call it by:
qsort(Array, 10, sizeof(int), compar);
And you'll do the compare function as usual:
int compar (const void* a, const void* b) {
return *((int*)a) - *((int*)b);
}
You need to understand that the values passed to compar are not the indexes but the address of the cells of your array. So you don't have to use Array from the compar function, you already have what you need.
You're already giving him your array by calling
qsort (a, numberOfElements, sizeOfEachElement, compare);
What you need to do in your compare function is this:
int compare (const void * a, const void * b)
{
//Here int can be other type
return ( *(int*)a - *(int*)b );
}
I already have a list of strings read in from a text file into a 2D array named word ready to be sorted.
The list looks like:
I
like
cherry
pie
and
chocolate
pie
I want the list to look like this after sorted:
and
cherry
chocolate
I
like
pie
pie
The function prototype is below. int counter is the amount of strings, and MAX_CHAR_LEN = 1024 in case you were wondering.
void alphabetize(char word[][MAX_CHAR_LEN], int counter)
{
return;
}
Notice that sorting by the first character alone is not sufficient, as the list contains two strings that start with "ch"
Can someone provide a function that can do this? Thanks in advance.
You want to use the qsort() function.
qsort(base, num_of_elements, element_size, my_compare);
The comparison function my_compare takes two arguments, each a const void *, and returns a number indicating the relative order of the arguments. A negative number means the first argument is before the second argument. A positive number means the first argument is after the second argument. A zero is returned if the arguments have compared to be equal.
As your string comparison is case insensitive, you will need to create your own comparison function, or find one provided to you by your system that is not part of the C library proper. POSIX provides strcasecmp() for this purpose (Google tells me that _stricmp() is available on Windows).
int my_compare (const void *a, const void *b) {
return strcasecmp(a, b);
}
Defining the comparison function is usually the trickiest part of using qsort(). You have to understand the context of the pointers that are being passed into that function. When an array of TYPE is passed into qsort(), it will pass a pointer to const TYPE to each argument of the comparison function.
In your case, you would be passing in an array of array of MAX_CHAR_LEN chars. So, each argument to the comparison function is a pointer to const array of MAX_CHAR_LEN chars. This means that technically, the my_compare function should be written like this:
int my_compare (const void *a, const void *b) {
typedef char TYPE[MAX_CHAR_LEN];
const TYPE *aa = (const TYPE *)a;
const TYPE *bb = (const TYPE *)b;
return strcasecmp(*aa, *bb);
}
The cast on the arguments would normally not be necessary, except that C doesn't really support the notion of a constant array. It converts such a thing into an array of constants, so the cast is required to reflect that.
However, the address of an array is equal to the address of its first element. That is, for the code above, the following assertions would be true:
assert(aa == (const void *)*aa);
assert(bb == (const void *)*bb);
So, because the dereference of a pointer to an array equals the decayed address value of the same array, the first implementation of my_compare() is sufficient for your 2-D array.
You can use the qsort function to sort. You also need to create a compare function that compares two arrays of chars, and then pass that function pointer as an argument.
Example that sorts ints:
/* qsort example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort */
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int n;
qsort (values, 6, sizeof(int), compare);
for (n=0; n<6; n++)
printf ("%d ",values[n]);
return 0;
}
The above code can easily be adapted to sort arrays of chars instead of ints.
If you want to write your own sort function, something like this is pretty straight forward.
for (int i = 0; i < array.size(); i++)
{
for (int j = i+1; j < array.size(); j++)
{
if (array[i] > array[j])
swap(array[i],array[j]);
}
}
qsort is Good Option. See it's detail here
You can also try Bubble Sort. It's implementation in C is easy - See this Good answer for help
Lets take qsort()'s comparison callback function as an example
int (*compar)(const void *, const void *)
What happens when the result of the comparison function depends on the current value of a variable? It appears my only two options are to use a global var (yuck) or to wrap each element of the unsorted array in a struct that contains the additional information (double yuck).
With qsort() being a standard function, I'm quite surprised that it does not allow for additional information to be passed in; something along the lines of execv()'s NULL-terminated char *const argv[] argument.
The same thing can be applied to other functions which define a callback that leave no headroom for additional parameters, ftw() & nftw() being two others I've had this problem with.
Am I just "doing it wrong" or is this a common problem and chalked up to an oversight with these types of callback function definitions?
EDIT
I've seen a few answers which say to create multiple callback functions and determine which one is appropriate to pass to qsort(). I understand this method in theory, but how would you apply it in practice if say I wanted the comparison callback function to sort an array of ints depending on how close the element is to a variable 'x'?. It would appear that I would need one callback function for each possible value of 'x' which is a non-starter.
Here is a working example using the global variable 'x'. How would you suggest I do this via multiple callback functions?
#include <stdint.h>
#include <stdio.h>
#include <math.h>
int bin_cmp(const void*, const void*);
int x;
int main(void)
{
int i;
int bins[6] = { 140, 100, 180, 80, 240, 120 };
x = 150;
qsort(bins, 6, sizeof(int), bin_cmp);
for(i=0; i < 6; i++)
printf("%d ", bins[i]);
return 0;
}
int bin_cmp(const void* a, const void* b)
{
int a_delta = abs(*(int*)a - x);
int b_delta = abs(*(int*)b - x);
if ( a_delta == b_delta )
return 0;
return a_delta < b_delta ? -1 : 1;
}
Output
140 180 120 100 80 240
Change the value of the function pointer. The whole point (no pun intended) of function pointers is that the user can pass in different functions under different circumstances. Rather that passing in a single function which acts differently based on external circumstances, pass in different functions based on the external circumstances.
This way, you only need to know about the values of variables in the context of the call to qsort() (or whatever function you're using), and write a couple different simpler comparison functions instead of one big one.
In response to your edit
To deal with the issue described in your update, just use to the global variable by name in your comparison function. This will certainly work if you are storing the variable at the global scope, and I believe qsort() will be able to find it at most other (public) scopes visible to the comparison function definition, as long as the scope is fully qualified.
However, this approach won't work if you want to pass a value straight into the sorting process without putting it in a variable.
It sounds like you need the bsearch_s() and qsort_s() functions defined by TR 24731-1:
§6.6.3.1 The bsearch_s function
Synopsis
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdlib.h>
void *bsearch_s(const void *key, const void *base,
rsize_t nmemb, rsize_t size,
int (*compar)(const void *k, const void *y, void *context),
void *context);
§6.6.3.2 The qsort_s function
Synopsis
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdlib.h>
errno_t qsort_s(void *base, rsize_t nmemb, rsize_t size,
int (*compar)(const void *x, const void *y, void *context),
void *context);
Note that the interface has the context that you need.
Something rather close to this should be available in the MS Visual Studio system.
With qsort's signature being what it is, I think your options are pretty limited. You can use a global variable or wrap your elements in structs as you suggested, but I'm not aware of any good "clean" way of doing this in C. I imagine there are other solutions out there, but they won't be any cleaner than using global variables. If your application is single-threaded, I would say this is your best bet as long as you're careful with the globals.
I'd listen to tlayton, and wrap that logic in a function that returns a pointer to the appropriate comparison function.
You can write your own specialized qsort implementation and customize it to your need.
To give you an idea about how short it is, this is a qsort implementation with your delta.
void swap(int *a, int *b)
{
int t=*a; *a=*b; *b=t;
}
void yourQsort(int arr[], int beg, int end, int delta)
{
if (end > beg + 1)
{
int piv = arr[beg], l = beg + 1, r = end;
while (l < r)
{
//Here use your var something like this
int a_delta = abs(arr[l] - delta);
int b_delta = abs(piv - delta);
if (a_delta <= delta)
l++;
else
swap(&arr[l], &arr[--r]);
}
swap(&arr[--l], &arr[beg]);
yourQsort(arr, beg, l, delta);
yourQsort(arr, r, end, delta);
}
}
More C-optimized implementations are said to be here.
You will have to prepare several function callbacks and check the value before running qsort, then send the correct one.
IMO, go with the global. The problem is that you're really asking qsort to do two things, both sort and map, where it's only designed to do the first.
The other thing you could do is break it down into a couple steps. First compute an array of these (one for each element of the source array):
struct sort_element {
int delta; // This is the delta value
int index; // This is the index of the value in the source array
}
Call qsort on that array, using a sort function that compares delta values. Then you use the index'es on the sorted array to order your original array. This consumes some memory, but it may not matter that much. (Memory is cheap, and the only thing you have to store in the temporary array is the key, not the entire value.)