473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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.
Nov 14 '05 #1
5 1826
nrk
Try Kret wrote:
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.
Faithfully yours,
Try Kret.


Nov 14 '05 #2
On Mon, 22 Dec 2003 23:58:50 -0500, Try Kret wrote:
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;
}
}
Nov 14 '05 #3
nrk <ra*********@deadbeef.verizon.net> wrote in message news:<wI*****************@nwrddc02.gnilink.net>...
Try Kret wrote:
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.
Faithfully yours,
Try Kret.


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?
Nov 14 '05 #4
Michael B Allen <mb*****@ioplex.com> wrote in message news:<pa**********************************@ioplex. com>...
On Mon, 22 Dec 2003 23:58:50 -0500, Try Kret wrote:
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;
}
}


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?
Nov 14 '05 #5
Try Kret wrote:

Michael B Allen <mb*****@ioplex.com> wrote in message news:<pa**********************************@ioplex. com>...
On Mon, 22 Dec 2003 23:58:50 -0500, Try Kret wrote:
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;
}
}


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);

--
pete
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
10
by: Ron_Adam | last post by:
I'm trying to figure out how to test function arguments by adding a decorator. @decorate def func( x): # do something return x This allows me to wrap and replace the arguments with my own,...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
5
by: mancomb | last post by:
Hi, I'm curious to the syntax of calling member functions through pointers of classes returned through the -operator. For example (excuse the crude incomplete code); Here are the classes: ...
14
by: Nickolay Ponomarev | last post by:
Hi, Why does http://www.jibbering.com/faq/ uses new Function constructor instead of function expressions (function(...) { ... }) when defining new functions? E.g. for LTrim and toFixed. Is the...
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.