How to write function that accepts comparator in C? - 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.

Related

How to sort an array of pointers in alphabetical order, and then use qsort?

I am trying to write a function in which I sort the pointers inside of wptrs, an array of pointers to strings in another array. I am challenging myself not to use string.h for this exercise, as I want to understand how a sorting algorithm could work in C. I am using qsort(), however, but I am trying to write a comparison function for it called mycharptrcompare().
I have looked at how strcmp() works, and I have tried to mimic that with mycharptrcompare(). However, I notice the difference that strcmp() expects a char*, while the mycharptrcompare() function expects a char**. I've written a method called dumpwptrs to show me the contents and how they are organized within wptrs. Thus far, I have the following code:
UPDATE:
I have also tried:
int mycharptrcompare(const void *a, const void *b)
{
//Need to convert a void * to a more specific type to dereference
const char *aPtr = a;
const char *bPtr = b;
const char **pa = &aPtr;
const char **pb = &bPtr;
while (*pa && *pa == *pb) {
pa++;
pb++;
}
return *pa - *pb;
}
and my output I got was:
(null)
jumps
world
is
dog
blue
Which is still incorrect, because my list should be sorted in alphabetical order, and the first input (the word "hello"), has not been read in.
FYI here is an example use of qsort().
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void *a, const void *b)
{
const char **pa = a;
const char **pb = b;
return strcmp(*pa, *pb);
}
int main(void)
{
char *wptrs[] = { "hello", "jumps", "world", "is", "dog", "blue" };
size_t len = sizeof wptrs / sizeof wptrs[0];
qsort(wptrs, len, sizeof wptrs[0], cmp);
for(size_t i = 0; i < len; i++) {
printf("%s\n", wptrs[i]);
}
return 0;
}
Program output:
blue
dog
hello
is
jumps
world

Strcmp causes segfault

Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int my_compare(const void * a, const void * b);
int main()
{
char s[][80] =
{ "gxydyv", "gdyvjv", "lfdtvr", "ayfdbk", "sqkpge", "axkoev", "wdjitd", "pyrefu", "mdafyu",
"zdgjjf", "awhlff", "dqupga", "qoprcn", "axjyfb", "hfrgjf", "dvhhhr" };
int i;
puts("#Before:#");
for (i = 0; i < 16; i++)
puts(s[i]);
qsort(s, 16, sizeof *s, my_compare);
putchar('\n');
puts("#After:#");
for (i = 0; i < 16; i++)
puts(s[i]);
return 0;
}
int my_compare(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}
Here is the output:
#Before:#
gxydyv
gdyvjv
lfdtvr
ayfdbk
sqkpge
axkoev
wdjitd
pyrefu
mdafyu
zdgjjf
awhlff
dqupga
qoprcn
axjyfb
hfrgjf
dvhhhr
Segmentation fault
I also notice that the prototype of strcmp is:
int strcmp(const char *s1,const char *s2);
I suppose that the type of a and b in my_compare is "pointer to array-of-char". As a result, *(char **)a is a "pointer to char", which is exactly what strcmp expects.
So where is the problem?
Change:
return strcmp(*(char **) a, *(char **) b);
To:
return strcmp(a,b);
You had an extra level of pointer dereferencing that was incorrect and that's why you got the segfault. That is, you were passing the char values and not the char pointers [which got masked with the cast].
Note: no need to cast from void * here.
UPDATE:
In reponse to your question, yes, because of the way you defined s and the qsort call.
Your original my_compare would have been fine if you had done:
char *s[] = { ... };
And changed your qsort call to:
qsort(s, 16, sizeof(char *), my_compare);
To summarize, here are two ways to do it
int
main()
{
char s[][80] = { ... }
qsort(s, 16, 80, my_compare);
return 0;
}
int
my_compare(const void *a, const void *b)
{
return strcmp(a,b);
}
This is a bit cleaner [uses less space in array]:
int
main()
{
char *s[] = { ... }
qsort(s, 16, sizeof(char *), my_compare);
return 0;
}
int
my_compare(const void *a, const void *b)
{
return strcmp(*(char **) a,*(char **) b);
}
UPDATE #2:
To answer your second question: No
None of these even compile:
return strcmp((char ()[80])a,(char ()[80])b);
return strcmp(*(char ()[80])a,*(char ()[80])b);
return strcmp((char [][80])a,(char [][80])b);
return strcmp(*(char [][80])a,*(char [][80])b);
But, even if the did, they would be logically incorrect. The following does not compile either, but is logically closer to what qsort is passing:
return strcmp((char [80])a,(char [80])b);
But, when a function passes something defined as char x[80] it's just the same as char *x, so qsort is passing char * [disguised as void *].
A side note: Using char *s[] is far superior. It allows for arbitrary length strings. The other form char s[][80] will actually fail if a given string exceeds [or is exactly] 80 chars.
I think it's important for you to understand:
Arrays are call by reference.
The interchangeability of arrays and pointers.
The following two are equivalent:
char *
strary(char p[])
{
for (; *p != 0; ++p);
return p;
}
char *
strptr(char *p)
{
for (; *p != 0; ++p);
return p;
}
Consider the following [outer] definitions:
char x[] = { ... };
char *x = ...;
Either of these two may be passed to strary and/or strptr in any of the following forms [total of 20]:
strXXX(x);
strXXX(x + 0);
strXXX(&x[0]);
strXXX(x + 1);
strXXX(&x[1]);
Also, see my recent answer here: Issue implementing dynamic array of structures
You can just cast it to a const char *, it should work now:
int my_compare(const void *a, const void *b) {
return strcmp((const char *)a, (const char *)b);
}
And also you should add:
#include <stdlib.h>

qsort Segmentation Fault structs

So, my first question here, please be patient with me:
My task is to sort an array of structs (name, surname and another struct for the birthday, which consists of the year, month, day). I have to sort by birthdate and by using qsort.
My problem is, I looked up everything about qsort but i am not quite sure if my implementation is correct since I am new to C. I can create the executable program but it is not giving my any result only Segmentation Fault.
Here is my Code:
#include <stdio.h>
#include <stdlib.h>
typedef int (*compfn) (const void*, const void*);
typedef struct {
unsigned year, month, day;
} date_t;
typedef struct {
char name[32];
char surname[32];
date_t birthday;
}person_t;
typedef struct {
unsigned n;
unsigned cap;
person_t *arr;
} persons_t;
int compare(person_t *a, person_t *b){
if(a->birthday.year!=b->birthday.year){
return a->birthday.year-b->birthday.year;
}else{
if(a->birthday.month!=b->birthday.month){
return a->birthday.month-b->birthday.month;
}else{
return a->birthday.day-b->birthday.day;
}
}
}
int main(int argc, char* argv[])
{
if (argc <= 1) {
fprintf(stderr, "syntax: %s <inputfile>\n", argv[0]);
return 1;
}
FILE* f = fopen(argv[1], "rt");
if (f == NULL) {
fprintf(stderr, "cannot open file %s\n", argv[1]);
return 1;
}
persons_t persons;
persons.n = 0;
persons.cap = 0;
persons.arr = NULL;
person_t p;
while (fscanf(f, "%s %s %4u-%2u-%2u", p.name, p.surname,
&p.birthday.year, &p.birthday.month, &p.birthday.day) == 5) {
if (persons.n == persons.cap) {
persons.cap = persons.cap == 0 ? 1 : 2 * persons.cap;
persons.arr = realloc(persons.arr, persons.cap * sizeof(persons.arr[0]));
}
persons.arr[persons.n++] = p;
}
int nitems = persons.cap*sizeof(persons.arr[0]);
int size = sizeof(persons.arr[0]);
qsort(persons.arr, nitems, size, (compfn)compare);
for (unsigned i = 0; i < persons.n; i++) {
person_t *p = persons.arr + i;
printf("%s %s %4u-%2u-%2u\n",
p->name, p->surname,
p->birthday.year, p->birthday.month, p->birthday.day);
}
fclose(f);
return 0;
}
I hope someone can help me,
Thanks in advance ;)
As far as _t-suffixed identifiers go, according to the C standard they're reserved for the implementation (e.g. your compiler, and/or your standard library). It's very possible that your implementation already has a date_t type, and your code might be causing some kind of mischief. If you wish to avoid subtly and dangerously clashing identifiers wreaking all sorts of havoc, it's probably best to avoid them. Not to worry, you could always use '_s' to denote a struct type instead!
Whenever you're declaring a variable that represents an index within an array, use size_t as the type!
int compare(person_t *a, person_t *b){
...
qsort(persons.arr, nitems, size, (compfn)compare);
According to the qsort manual, the argument given as the comparator function should be an int (*compar)(const void *, const void *), and that's what you've given since you've cast to (compfn). As far as qsort is aware that function accepts two const void * arguments, which might differ in representation to person_t * arguments. This could certainly cause segfaults. Don't lie about the type of compare. Change it to look more like:
int compare(const void *x, const void *y) {
const person_s *a = x, *b = y;
/* ... */
}
... and you won't need the cast or the typedef.
Next, onto return values for that function. I have used implementations where-by lexically illogical return values cause segmentation faults. For example, if a <= b and b <= c, then a <= c, but your code doesn't guarantee this. In fact, using your code it is possible that a <= b, b <= c and a > c. I recommend making sure your code guarantees correspondence between the return value and lexical order. You can do so by returning 1 for greater than, 0 for equal to or -1 for less than.
#define lexical_order(x,y) ((x > y) - (x < y))
int compare(const void *x, const void *b){
const person_s *a = x, *b = y;
return a->birthday.year != b->birthday.year ? lexical_order(a->birthday.year, b->birthday.year)
: a->birthday.month != b->birthday.month ? lexical_order(a->birthday.month, b->birthday.month)
: lexical_order(a->birthday.day, b->birthday.day);
}
I'm sure you're aware that you should be checking the return value of realloc... For example:
void *temp = realloc(persons.arr, persons.cap * sizeof(persons.arr[0]));
if (temp == NULL) { /* If we don't check return value prior *
* to assigning to persons.arr, we *
* might leak some memory... */
puts("Error in realloc");
free(persons.arr);
exit(-1);
}
persons.arr = temp;
Finally, and most importantly (this is probably your error), are you sure about this?
int nitems = persons.cap*sizeof(persons.arr[0]);
If you mean to pass this as the number of items to qsort (which is usual), then I think that should be:
size_t nitems = persons.n;
P.S. In case you missed it the second time, you should probably audit your code to make sure you're using size_t to store array indexes only.
P.P.S. Don't forget to free(persons); at the end of your program, so you don't end up with reports of memory leaks when you use valgrind...
P.P.P.S. valgrind is awesome!
So you are allocating our array by doubling its size whenever needed, using persons.cap, but you are not filling all its elements, are you?
From your code, the actual number of persons is nitems = persons.n, not persons.cap. What if you retry your code with nitems=persons.n?
If you have unfilled elements in your array, it means the strings inside them are arbitrary (i.e person.name), so probably not null-terminated, and the crash will occur when you try to display them.

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.

Returning a 2D char array in C

I messed around with this enough but I really don't get it.
Here is what I want to do: Take a 2D char array as an input in a function, change the values in it and then return another 2D char array.
That's it. Quite simple idea, but ideas do not get to work easily in C.
Any idea to get me started in its simplest form is appreciated. Thanks.
C will not return an array from a function.
You can do several things that might be close enough:
You can package your array in struct and return that. C will return structs from functions just fine. The downside is this can be a lot of memory copying back and forth:
struct arr {
int arr[50][50];
}
struct arr function(struct arr a) {
struct arr result;
/* operate on a.arr[i][j]
storing into result.arr[i][j] */
return result;
}
You can return a pointer to your array. This pointer must point to memory you allocate with malloc(3) for the array. (Or another memory allocation primitive that doesn't allocate memory from the stack.)
int **function(int param[][50]) {
int arr[][50] = malloc(50 * 50 * sizeof int);
/* store into arr[i][j] */
return arr;
}
You can operate on the array pointer passed into your function and modify the input array in place.
void function(int param[][50]) {
/* operate on param[i][j] directly -- destroys input */
}
You can use a parameter as an "output variable" and use that to "return" the new array. This is best if you want the caller to allocate memory or if you want to indicate success or failure:
int output[][50];
int function(int param[][50], int &output[][50]) {
output = malloc(50 * 50 * sizeof int);
/* write into output[i][j] */
return success_or_failure;
}
Or, for the caller to allocate:
int output[50][50];
void function(int param[][50], int output[][50]) {
/* write into output[i][j] */
}
You cannot return an array from a function.
You have several options:
wrap arrays inside structs
struct wraparray {
int array[42][42];
};
struct wraparray foobar(void) {
struct wraparray ret = {0};
return ret;
}
pass the destination array, as a pointer to its first element (and its size), to the function; and change that array
int foobar(int *dst, size_t rows, size_t cols, const int *src) {
size_t len = rows * cols;
while (len--) {
*dst++ = 42 + *src++;
}
return 0; /* ok */
}
// example usage
int x[42][42];
int y[42][42];
foobar(x[0], 42, 42, y[0]);
change the original array
int foobar(int *arr, size_t rows, size_t cols) {
size_t len = rows * cols;
while (len--) *arr++ = 0;
return 0; /* ok */
}
char **foo(const char * const * bar, size_t const *bar_len, size_t len0) {
size_t i;
char** arr = malloc(sizeof(char *) * len0);
for (i = 0; i < len0; ++i) {
arr[i] = malloc(bar_len[i]);
memcpy(arr[i], bar[i], bar_len[i]);
}
/* do something with arr */
return arr;
}
Somewhere else in your code:
char **pp;
size_t *pl;
size_t ppl;
/* Assume pp, pl are valid */
char **pq = foo(pp, pl, ppl);
/* Do something with pq */
/* ... */
/* Cleanup pq */
{
size_t i;
for (i = 0; i < ppl; ++i)
free(pq[i]);
free(pq);
}
Because you're passing by-pointer instead of by-value and you want to write to the input array, you have to make a copy of it.
Here's another example. Tested and works.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void test(char**,unsigned int,unsigned int);
const unsigned int sz_fld = 50 + 1;
const unsigned int sz_ffld = 10;
int main(void) {
char fld[sz_ffld][sz_fld];
for (unsigned char i=0;i<sz_ffld;++i) {
strcpy(fld[i],"");
}
strcpy(fld[0],"one");
strcpy(fld[1],"two");
strcpy(fld[2],"three");
char** pfld = malloc(sz_ffld*sizeof(char*));
for (unsigned int i=0;i<sz_ffld;++i) {
*(pfld+i) = &fld[i][0];
}
test(pfld,sz_ffld,sz_fld);
printf("%s\n",fld[0]);
printf("%s\n",fld[1]);
printf("%s\n",fld[2]);
free(pfld);
return(0);
}
void test(char** fld,unsigned int m,unsigned int n) {
strcpy(*(fld+0),"eleven");
strcpy(*(fld+1),"twelve");
return;
}
Note the following:
For compiling, I am using gcc with the C99 option.
I defined the function to include the two sizes information, but I wrote very basic code and am not actually using the information at all, just the strcpy(), so this certainly is not security-safe code in any way (even though I'm showing the "m" and "n" for such facility). It merely shows a technique for making a static 2D char array, and working with it in a function through the intermediate of an array of pointers to the "strings" of the array.
When you pass a 2D array to a function as a parameter, you need to explicitly tell it the size of the arrays second dimension
void MyFunction(array2d[][20]) { ... }
The following will do what you want. it will print "One" and "Ten". Also note that it is typed to the exact array dimensions of 10 and 8.
char my_array[10][8] =
{
{"One"},
{"Two"},
{"One"},
{"One"},
{"One"},
{"One"},
{"One"},
{"One"},
{"Nine"},
{"Ten"},
};
void foo ( char (**ret)[10][8] )
{
*ret = my_array;
}
void main()
{
char (*ret)[10][8];
foo(&ret);
printf("%s\r\n", (*ret)[0] )
printf("%s\r\n", (*ret)[9] )
}
The original question was about RETURNING the array, so I'm updating this to show returning a value. You can't "return an array" directly, but you CAN make a typedef of an array and return that...
char my_array[10][8];
typedef char ReturnArray[8];
ReturnArray* foo()
{
return my_array;
}

Resources