How to create a function that can be applied to all type of variables?

T

Try Kret

Hello !

I was wondering how to create a function that can be applied to all
type of variables?
If we use for example, sortList(void *obj), we can not do the
comparison of 2 strcut.

Faithfully yours,
Try Kret.
 
N

nrk

Try said:
Hello !

I was wondering how to create a function that can be applied to all
type of variables?

Not exactly sure what you want here. Do you want to pass just a pointer of
any type to your function? If so, void * coupled with another formal
parameter that tells you the type is one easy approach. There are no
"magic" solutions here. You have to slog it out and write the code for
both passing the type as well as the data and handling both inside your
function. For instance:

int sortList(int type, void *obj) {
switch ( type ) {
case 1:
{ /* obj has type pointer-to-int */
int *data = obj;
...
}
break;
case 2:
{
/* obj has type pointer-to-float */
float *data = obj;
...
}
break;
...
}
}

void doStuff() {
int *intlist;

...

sortList(1, intlist);
}

You get the idea. If you're tempted to do this however, I would like you to
consider writing separate functions to handle each type rather than this
approach.

If all you're looking for is general purpose sorting of data, consider using
the library's qsort function. All you need to provide is the comparison
function for the data you want to sort. For instance, to sort an array of
integers:

#include <stdio.h>

int cmpints(const void *in1, const void *in2) {
const int *i1 = in1;
const int *i2 = in2;

if ( *i1 < *i2 )
return -1;
if ( *i1 > *i2 )
return 1;
return 0;
}

void sortList(size_t sz, int *list) {

qsort(list, sz, sizeof *list, cmpints);

}

If you need to pass an arbitrary number of parameters of arbitrary types, a
format string based varargs function along the lines of the *printf/*scanf
family can be used.
If we use for example, sortList(void *obj), we can not do the
comparison of 2 strcut.

strcut? Did you mean strcmp? You can write a wrapper function that calls
strcmp, and then use it along with qsort.

-nrk.
 
M

Michael B Allen

Hello !

I was wondering how to create a function that can be applied to all type
of variables?
If we use for example, sortList(void *obj), we can not do the comparison
of 2 strcut.

If void * is a pointer to an array of pointers you would then know
that each element is sizeof(void *). You could then provide for the
specification of a comparison function like:

typedef int (*compare_fn)(const void *object1, const void *object2);

and then call sortList(void *obj, compare_fn cmp).

Or you could specify the size of each element and use a comparison
function like sortList(void *obj, size_t elemsiz, compare_fn cmp). This
would be *very roughly*:

void
sortList(void *obj, size_t elemsiz, compare_fn cmp)
{
unsigned char *p1 = obj, *p2;

sorting loop {
compare_fn((void *)p1, p2)
p1 += elemsiz;
}
}
 
T

Try Kret

nrk said:
Not exactly sure what you want here. Do you want to pass just a pointer of
any type to your function? If so, void * coupled with another formal
parameter that tells you the type is one easy approach. There are no
"magic" solutions here. You have to slog it out and write the code for
both passing the type as well as the data and handling both inside your
function. For instance:

int sortList(int type, void *obj) {
switch ( type ) {
case 1:
{ /* obj has type pointer-to-int */
int *data = obj;
...
}
break;
case 2:
{
/* obj has type pointer-to-float */
float *data = obj;
...
}
break;
...
}
}

void doStuff() {
int *intlist;

...

sortList(1, intlist);
}

You get the idea. If you're tempted to do this however, I would like you to
consider writing separate functions to handle each type rather than this
approach.

If all you're looking for is general purpose sorting of data, consider using
the library's qsort function. All you need to provide is the comparison
function for the data you want to sort. For instance, to sort an array of
integers:

#include <stdio.h>

int cmpints(const void *in1, const void *in2) {
const int *i1 = in1;
const int *i2 = in2;

if ( *i1 < *i2 )
return -1;
if ( *i1 > *i2 )
return 1;
return 0;
}

void sortList(size_t sz, int *list) {

qsort(list, sz, sizeof *list, cmpints);

}

If you need to pass an arbitrary number of parameters of arbitrary types, a
format string based varargs function along the lines of the *printf/*scanf
family can be used.


strcut? Did you mean strcmp? You can write a wrapper function that calls
strcmp, and then use it along with qsort.

-nrk.

Thank you very much. I did not mean strcmp, instead I mean we can not
compare 2 structure data type. If we pass structure as arguments, is
there any problem we the instruction of comparison is executed?
ex: struct elt{
void *obj;
elt *next;
};
typedef (struct elt *) list;
list *l;

Can we call: void sortList(*l); without error?
 
T

Try Kret

Michael B Allen said:
If void * is a pointer to an array of pointers you would then know
that each element is sizeof(void *). You could then provide for the
specification of a comparison function like:

typedef int (*compare_fn)(const void *object1, const void *object2);

and then call sortList(void *obj, compare_fn cmp).

Or you could specify the size of each element and use a comparison
function like sortList(void *obj, size_t elemsiz, compare_fn cmp). This
would be *very roughly*:

void
sortList(void *obj, size_t elemsiz, compare_fn cmp)
{
unsigned char *p1 = obj, *p2;

sorting loop {
compare_fn((void *)p1, p2)
p1 += elemsiz;
}
}

Talk about this function:
typedef int (*compare_fn)(const void *object1, const void *object2);

For example:
struct student{
int id;
char name[20];
int age;
}stud1, stud2;

If I call: compare_fn((struct student)stud1, (struct student)stud2);
what will be the problem?
 
P

pete

Try said:
Michael B Allen said:
If void * is a pointer to an array of pointers you would then know
that each element is sizeof(void *). You could then provide for the
specification of a comparison function like:

typedef int (*compare_fn)(const void *object1, const void *object2);

and then call sortList(void *obj, compare_fn cmp).

Or you could specify the size of each element and use a comparison
function like sortList(void *obj, size_t elemsiz, compare_fn cmp). This
would be *very roughly*:

void
sortList(void *obj, size_t elemsiz, compare_fn cmp)
{
unsigned char *p1 = obj, *p2;

sorting loop {
compare_fn((void *)p1, p2)
p1 += elemsiz;
}
}

Talk about this function:
typedef int (*compare_fn)(const void *object1, const void *object2);

For example:
struct student{
int id;
char name[20];
int age;
}stud1, stud2;

If I call:
compare_fn((struct student)stud1, (struct student)stud2);
what will be the problem?

compare_fn(&stud1, &stud2);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top