Can a function pointer be passed as an argument without its parameters? - c

So, there is function qsort in stdlib.h and its prototype is as follows;
void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));
As you can see, it needs a function function pointer as its last argument.
compare function is defined as follows:
int compare(const void *a, const void *b){
return (*(int*)a - *(int*)b);
}
now when I call qsort, I only to declare function pointer like
int (*cmp)(const void*, const void*) = &compare;
and pass cmp as the last argument to the function, along with the other parameters. No need to specify the parameters of the function compare.
Here's is my main function
main(){
int (*cmp) (const void* , const void*);
cmp = &compare;
int iarray[] = {1,2,3,4,5,6,7,8,9};
qsort(iarray, sizeof(iarray)/sizeof(*iarray), sizeof(*iarray), cmp);
int c = 0;
while (c < sizeof(iarray)/sizeof(*iarray)){
printf("%d \t", iarray[c]);
c++;
}
}
I got this code online and just cant figure why I don't need to pass any parameters to the function pointer cmp.

That's because the parameters are passed within qsort function, the function that you passed the function pointer to. Like:
/*
* qsort.c
*
* This is actually combsort. It's an O(n log n) algorithm with
* simplicity/small code size being its main virtue.
*/
...
void qsort(void *base, size_t nmemb, size_t size,
int (*compar) (const void *, const void *))
{
size_t gap = nmemb;
size_t i, j;
char *p1, *p2;
int swapped;
if (!nmemb)
return;
do {
gap = newgap(gap);
swapped = 0;
for (i = 0, p1 = base; i < nmemb - gap; i++, p1 += size) {
j = i + gap;
if (compar(p1, p2 = (char *)base + j * size) > 0) {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here calls the compare function
//
memswap(p1, p2, size);
swapped = 1;
}
}
} while (gap > 1 || swapped);
}
Function pointers are usually passed without parameters, since most of time function pointers are used to accomplish callback (see https://en.wikipedia.org/wiki/Callback_(computer_programming)). If the parameters are passed together, it should be intended to implement lazy evaluation.

Related

type-agnostic belongs function

I'm trying to build a function that checks whether a particular pointer value is stored in a given array. I'm trying to make the function type-agnostic and so I decided to go with the approach that was used to implement qsort(), in which a function pointer is passed to do the type-specific tasks.
The function looks like the following:
int is_in(void* array, int size, void* pelement, int (*equals)(void* this, void* that)) {
for(int k = 0; k < size; k++) {
if(equals(array + k, pelement)) {
return 1;
}
}
return 0;
}
The equals() function checks whether the second parameter is equal to the value pointed at by the first parameter.
One particular implementation of the equals() function that I needed to realize pertains to a struct Symbol type that I created. The implementation looks like the following:
int ptreq(void* ptr1, void* ptr2) {
return ((*((Symbol**) ptr1) == (Symbol*) ptr2));
}
The struct Symbol is defined as follows:
enum SymbolType {
TERMINAL,
NONTERMINAL
} typedef SymbolType;
struct Symbol {
char* content;
SymbolType type;
} typedef Symbol;
void set_symbol(Symbol* pS, SymbolType type, char* content) {
pS->content = malloc(sizeof(content));
strcpy(pS->content, content);
pS->type = type;
}
However, when I tried testing is_in() with a base example, I ended up with incorrect results. For instance, the following code:
#include <stdlib.h>
#include <stdio.h>
#include "string.h"
#include <stdarg.h>
#include <unistd.h>
int main(int argc, char* argv[]) {
Symbol F, E;
set_symbol(&E, NONTERMINAL, "E");
set_symbol(&F, NONTERMINAL, "F");
Symbol** pptest = malloc(2*sizeof(Symbol*));
pptest[0] = &E;
pptest[2] = &F;
printf("Is F in pptest? %d\n", is_in(pptest, 2, &F, &ptreq));
return 0;
}
Gives the following Output:
Is F in pptest? 0
Even though &F is within pptest.
What could be the problem with this approach?
Type void is an incomplete type. So used by you the expression array + k with the pointer arithmetic in the if statement
if(equals(array + k, pelement)) {
is invalid.
Also you need to pass to the function the size of objects stored in the array that will be used in expressions with the pointer arithmetic.
Using your approach the function should be declared similarly to standard C function bsearch that looks like
void *bsearch(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
Only the return type must be changed from void * to int.
That is the declaration pf your function will look like
int is_in( const void *pvalue,
const void *array,
size_t nmemb,
size_t size,
int cmp( const void *, const void *) );
The function can be defined the following way
int is_in( const void *pvalue,
const void *array,
size_t nmemb,
size_t size,
int cmp( const void *, const void *) )
{
size_t i = 0;
while ( i < nmemb && cmp( pvalue, ( const char * )array + i * size ) != 0 ) i++;
return i != nmemb;
}
In general the comparison function shall return an integer less than, equal to, or greater than zero if the searched element is considered, respectively, to be less than, to match, or to be greater than the array element.
In your case as you have an array of pointers that can point to arbitrary objects then the function should return 0 if elements passed to the function are equal each other or just a positive value if they are unequal each other.
int ptreq( const void *ptr1, const void *ptr2 )
{
return *( const Symbol ** )ptr1 != *( const Symbol ** )ptr2;
}
Pay attention to that the passed searched elementmust have the typeSymbol **`.
Here is a demonstration program.
#include <stdio.h>
int is_in( const void *pvalue,
const void *array,
size_t nmemb,
size_t size,
int cmp( const void *, const void * ) )
{
size_t i = 0;
while (i < nmemb && cmp( pvalue, ( const char * )array + i * size ) != 0) i++;
return i != nmemb;
}
int cmp_ptr( const void *ptr1, const void *ptr2 )
{
return *( const int ** )ptr1 != *( const int ** )ptr2;
}
int main( void )
{
int x, y, z;
int * a[] = { &x, &y, &z };
const size_t N = sizeof( a ) / sizeof( *a );
int *pvalue = &y;
printf( "&y is in the array = %s\n",
is_in( &pvalue, a, N, sizeof( *a ), cmp_ptr ) ? "true" : "false" );
int v;
pvalue = &v;
printf( "&v is in the array = %s\n",
is_in( &pvalue, a, N, sizeof( *a ), cmp_ptr ) ? "true" : "false" );
}
The program output is
&y is in the array = true
&v is in the array = false
A Symbol** passed to a void* parameter doesn't come out as an array in the other end unless you cast it to a proper type. array + k is invalid C and will not compile cleanly on conforming compilers. You cannot do pointer arithmetic on void* nor can you iterate through what it points at without knowing the item size - there's a reason why qsort takes that as parameter.
A correctly written standard C function might look something like this:
#include <stddef.h>
#include <stdbool.h>
bool is_in (const void* array,
size_t n_items,
size_t item_size,
const void* element,
int (*equals)(const void*, const void*))
{
unsigned char* byte = array;
for(size_t i=0; i<n_items; i++)
{
if(equals(&byte[i*item_size], element))
{
return true;
}
}
return false;
}
int symbol_equal (const void* obj1, const void* obj2)
{
const Symbol* s1 = obj1;
const Symbol* s2 = obj2;
...
// in case you passed an array of pointers, then an extra level of dereferencing here
}
As others have pointed out, the problem results from trying to perform addition on void*, which is not defined in standard C. While other answers avoid this by passing the item size, as is the case with qsort(), I managed to solve the problem by separating array and k into distinct parameters of the equals() routine, which then performs pointer arithmetic after casting into the proper, non-void* type.
int is_in(void* list, int size, void* pelement, int (*equals)(void* this, int k, void* that)) {
for(int k = 0; k < size; k++) {
if(equals(list, k, pelement)) {
return 1;
}
}
return 0;
}
int ptreq(void* ptr1, int k, void* ptr2) {
return (*(((Symbol**) ptr1) + k) == (Symbol*) ptr2);
}

How to write function that accepts comparator in C?

I have a struct vector that holds it's data in a double void pointer. The struct looks like this:
typedef struct vector {
void **data;
int capacity;
int size;
} vector;
I am trying to somewhat replicate the std::find function in C++ to find items in the vector that I have. I have done this by creating a function vector_find:
int vector_find(vector* v, void *elem, __compar_fn_t cmp)
{
for (int i = 0; i < v->size - 1; i++)
{
if(cmp(v->data[i], elem) == 0)
{
return i;
}
}
return -1;
}
This function is found in my vector.c file. It accepts a comparator function. If it finds the element, it will return its location in the vector.
This is all defined in my vector.c file.
Now I am trying to use this function in one of my programs, in this function:
int cstring_cmp(const void *a, const void *b)
{
const char *ia = *(const char **)a;
const char *ib = *(const char **)b;
printf("%s %s", ia, ib);
return strcmp(ia, ib);
}
void
execute(vector* tokens)
{
if (vector_find(tokens, ";", cstring_cmp) > -1)
{
printf("semicolon found");
}
}
However, when I run this function, I get a segfault with the following error:
Program received signal SIGSEGV, Segmentation fault.
__strlen_sse2 () at ../sysdeps/x86_64/multiarch/../strlen.S:120
120 ../sysdeps/x86_64/multiarch/../strlen.S: No such file or directory.
A couple of things I have verified:
I have verified that the error occurs in my execute function. Specifically when I call vector_find.
I have verified that this error comes up whether or not tokens is empty. This is interesting because the for-loop in vector_find should never execute if the vector I pass to vector_find is empty.
I have included in both vector.c and the file I am running.
I have also tried this format:
int vector_find(vector* v, void *elem, int (*cmp) (const void*, const void*))
{
for (int i = 0; i < v->size; i++)
{
if((*cmp) (v->data[i], elem) == 0)
{
return i;
}
}
return -1;
}
...and got the same error.
Am I doing this right? Obviously not - where does my error lie?
Your comparison function is expecting that each of its parameters is a char **. However, you call vector_find with a char * being passed as the second parameter which is subsequently passed to cmp. Attempting to use a char * as a char ** invokes undefined behavior as you end up passing to strcmp is actually a char.
You don't show how you populate your vector, but I'm guessing the elements of data are of type char *. That being the case, the comparison function should be expecting a char * instead of a char **.
int cstring_cmp(const void *a, const void *b)
{
const char *ia = a;
const char *ib = b;
printf("%s %s", ia, ib);
return strcmp(ia, ib);
}
If I'm reading this right, you've written a comparator that pretends a vector is a const char*. It isn't. The subsequent C-string comparison is likely not to hit a would-be null terminator and is going to go out of bounds.
Instead, write a comparator that actually compares vector objects to whatever it is that you want to compare them to.

C - qsort sends the wrong pointers to the comparator function?

My problem is that qsort seems to be sending weird pointers to the comparator function. The comparator function itself seems to be working fine if I create 2 gaps and send their pointers as arguements. However, when debugging I'm getting wrong values, even though the gap array is initialized correctly.
I am running the code on Windows 10 if that matters.
Gap definition and comparator function:
typedef struct open_space_t{
ssize_t size;
off_t start;
}Gap;
int GapComparator(const void * aa, const void * bb){
ssize_t a = ((Gap*) aa)->size;
ssize_t b = ((Gap*) bb)->size;
if(a>b){
return 1;
}
if(b>a){
return -1;
}
else{
return 0;
}
}
Running qsort:
Gap** allGaps = malloc((2) * sizeof(*allGaps));
allGaps[0] = malloc(sizeof(*allGaps[0]));
allGaps[0]->size = 20;
allGaps[0]->start = 30044;
allGaps[1] = malloc(sizeof(*allGaps[0]));
allGaps[1]->size = 20;
allGaps[1]->start = 30064;
qsort(allGaps, 2, sizeof(*allGaps), GapComparator);
The comparator receives pointers to the elements it should compare. Your array elements are pointers (Gap *). Therefore the comparator receives Gap **.
Fix:
int GapComparator(const void * aa, const void * bb){
ssize_t a = (*(Gap**) aa)->size;
ssize_t b = (*(Gap**) bb)->size;
Or alternatively:
int GapComparator(const void *aa, const void *bb) {
const Gap **pa = aa, **pb = bb;
ssize_t a = (*pa)->size;
ssize_t b = (*pb)->size;

How can I pass a variable to a function here?

I use qsort from stdlib.h,
void qsort (void* base, size_t num, size_t size,
int (*compar)(const void*,const void*));
in the following way:
void myfun (float *arr, int n, float c) // value of c is changeable
{
...// some code
qsort(float *arr, n, sizeof(float), compareme);
...// some code
}
with
int compareme (const void * a, const void * b)
{
float tmp = f((float*)a, (float*)b, c ); // f is some function, and how can I pass c here?
if (tmp < 0) return -1;
if (tmp == 0) return 0;
if (tmp > 0) return 1;
}
how can I make c usable in compareme here?
Thanks!
Many resort to using a (nasty) global variable.
It is too bad that qsort() doesn't include an extra void pointer argument that is just passed to the user-supplied compar() function. I ended up writing my own qsort() to overcome this limitation.
Prototype:
int myQsort(
void *arrayBase,
size_t elements,
size_t elementSize,
int(*compar)(const void *, const void *, void *callerArg),
void *callerArg
);
This allows me to pass all kinds of structures (cast to void *) to my compar() fn.
If you happen to be using Glibc (i.e., Linux) you can use qsort_r:
int compareme (const void *a, const void *b, void *data)
{
float c = *(float *)data;
float tmp = f((float*)a, (float*)b, c);
if (tmp < 0) return -1;
if (tmp == 0) return 0;
if (tmp > 0) return 1;
}
then call
qsort_r(float *arr, n, sizeof(float), compareme, &c);
You have to define the preprocessor macro _GNU_SOURCE before including any headers to get this (e.g. with -D_GNU_SOURCE) to get this function, and it will restrict your program's portability.
Otherwise, you'll have to use global or thread-local storage, or write your own sort function.

Sorting any kind of element using void pointers in C

Hello everyone i am writing a program for sorting general element in C. it can sort any type of object(int,float,complex number, objects)
What i have thought of is using void pointers,
void qsort(void *ptr,int sz,int i,int j,int (*fptr) (const void *,const void *) )
{
if(i<j)
{
int p=(i+j)/2;
p=partition(ptr,sz,i,j,p,fptr);
qsort(ptr,size,i,p-1,fptr);
qsort(ptr,size,p+1,j,fptr);
}
}
FOR Comparison
By the value of sz we will know that whether its a pointer to string,int,char,float,etc
int compare(const void* a,const void* b,int sz)
{
if(sz==0) //means pointer to a string
return strcmp( (char*)a, (char*)b );
else if(sz==1) //means int
return *(int*)a - *(int*)b;
else if(sz==2) //means float
return *(float*)a- *(float*)b;
else if(sz==3)
return *(char*)a- *(char*)b;
}
FOR SWAPPING TWO ELEMENTS
void swap(void *a,void *b,int sz)//for swapping
{
if(sz==0)
{
void *c;
c=a;
a=b;
b=c;
}
else if(sz==1)
{
a=(int*)a;
b=(int*)b;
int c;
c= *a;
*a=*b;
*b=c;
}
else if(sz==2)
{
a=(float*)a;
b=(float*)b;
float c;
c= *a;
*a=*b;
*b=c;
}
EDITED
qsort(arr,4,0,9,&compare);
The full code is under construction, please tell me if there could be some optimizations in my approach, or some better alternatives for this problem.
As it seems to me that it is really going to be big in size
Many many thanx in advance
Since your swap routine will likely be used by the partition function, it should work with arbitrary sized objects, not just the ones you plan to pass in to the code.
void swap (void *a, void *b, int sz) {
char buf[512];
void *p = buf;
if (sz > sizeof(buf)) p = malloc(sz);
memcpy(p, a, sz);
memcpy(a, b, sz);
memcpy(b, p, sz);
if (p != buf) free(p);
}
From the way you have written your comparison routine, it seems you only plan to send in certain types of arrays. But, sz is usually used to tell how big the individual elements in the array are, not as a type identifier, as you seem to be trying to use it.
struct x { int key; /*...*/ };
int cmp_x (const void *a, const void *b) {
const struct x *xa = a;
const struct x *xb = b;
return (xa->key > xb->key) - (xa->key < xb->key);
}
struct x array_x[100];
/* populate array */
qsort(array_x, sizeof(struct x), 0, 100, cmp_x);
This is how I imagine your qsort should be called. (Thanks to Ambroz Bizjak for the nifty comparison implementation.)
For an array of int:
int cmp_int (const void *a, const void *b) {
int ia = *(const int *)a;
int ib = *(const int *)b;
return (ia > ib) - (ia < ib);
}
int array_i[100];
/* populate array */
qsort(array_i, sizeof(int), 0, 100, cmp_int);
The problem is that this does not allow sorting of custom types, like structs. The usual approach is to accept a function pointer which you call to do the comparisons.
What you should be doing is passing in the comparison as a function pointer. You are passing in a function pointer but you don't seem to be using it to compare the values. You don't have to predefine all of the comparisons because you can define them when you use them, for the type of values you're using.

Resources