I have this function "cost_compare" that I would like to offload on FPGA for some experimental purposes. This function, how it is called and its arguments are as follows.
The synthesis tool doesn't accept double pointers as arguments for HW functions (in fact it is very picky about using pointers especially to data structures).
How do I get rid of the pointers in the function argument list? In other words, how do I convert pointers in this example to values? How does this possible solution affect the call by reference performed by spec_qsort?
Thanks in advance
Hooman
typedef struct arc *arc_p;
typedef LONG cost_t;
typedef struct basket
{
arc_t *a;
cost_t cost;
cost_t abs_cost;
LONG number;
} BASKET;
/* ... */
typedef struct arc
{
int id;
cost_t cost;
node_p tail, head;
short ident;
arc_p nextout, nextin;
flow_t flow;
cost_t org_cost;
} arc;
/* ... */
extern int cost_compare( BASKET **b1, BASKET **b2 );
/* ... */
int cost_compare( BASKET **b1, BASKET **b2 )
{
if( (*b1)->abs_cost < (*b2)->abs_cost )
return 1;
if( (*b1)->abs_cost > (*b2)->abs_cost )
return -1;
if( (*b1)->a->id > (*b2)->a->id )
return 1;
else
return -1;
}
/* ... */
spec_qsort(perm + 1, basket_sizes[thread], sizeof(BASKET*),
(int (*)(const void *, const void *))cost_compare);
/* ... */
BASKET* max, *act;
for (j = 1; j < num_threads; j++) {
act = *perm_p[j];
if (act->number >= 0) {
if (!max || cost_compare(&act, &max) < 0) {
max = act;
max_pos = j;
}
}
/* ... */
BASKET* max_basket;
static BASKET **opt_basket;
for (i = 0; i< num_threads; i++) {
if ((!max_basket && opt_basket[i]) || (opt_basket[i] &&
cost_compare(&opt_basket[i], &max_basket) < 0)) {
max_basket = opt_basket[i];
}
}
/* ... */
=========================================
Thanks #Gerardo Zinno. When I run in SW, your approach (in the last paragraph) works fine. However, when I synthesis 'cost_compare' on FPGA using Xilinx SDSoC, it only works for
if(b1->abs_cost < b2->abs_cost)
but not for
if( b1->a->id > b2->a->id )
and the tools gives me this error:
ERROR: [SYNCHK 200-61] /home/a1083898/Xilinx_examples/original_routeplanning/src/pbeampp.c:85: unsupported memory access on variable 'x' which is (or contains) an array with unknown size at compile time.
ERROR: [SYNCHK 200-41] /home/a1083898/Xilinx_examples/original_routeplanning/src/pbeampp.c:89: unsupported pointer reinterpretation from type 'i8*' to type '%struct.arc.1.4.6 = type { i32, i64, %struct.node.0.3.5*, %s...' on variable 'x'.
ERROR: [SYNCHK 200-11] /home/a1083898/Xilinx_examples/original_routeplanning/src/pbeampp.c:89: Argument 'x' has an unsynthesizable type 'i8*' (possible cause(s): pointer to pointer or global pointer).
ERROR: [SYNCHK 200-11] /home/a1083898/Xilinx_examples/original_routeplanning/src/pbeampp.c:89: Argument 'x' has an unsynthesizable type 'i8*' (possible cause(s): structure variable cannot be decomposed due to (1) unsupported type conversion; (2) memory copy operation; (3) function pointer used in struct; (4) unsupported pointer comparison).'
Also when called by 'qsort' as
qsort(perm + 1, basket_sizes[thread], sizeof(BASKET*), cost_compare);
I receive this warning:
warning: incompatible pointer types passing 'int (void *, void *)' to parameter of type '__compar_fn_t' (aka 'int (*)(const void *, const void *)') [-Wincompatible-pointer-types]
I know this error has nothing to do with C programming but if there is any way to get rid of 'b1->a->id' and 'b2->a->id', I believe the problem could be solved with the HW synthesis tool.
Kind regards
Hooman
In the int cost_compare( BASKET **b1, BASKET **b2 ) you don't need double pointers since you are just comparing elements and not swapping anything. (In fact notice that you aren't using b1 directly, but you're always dereferencing it)
Just change the function signature to int cost_compare( BASKET *b1, BASKET *b2 ). In the function's body change every (*b1)->abs_const to b1->abs_const.
Also, since spec_qsort expects a function with signature int compare (void *, void *), you could get rid of this cast "(int (*)(const void *, const void *)) cost_compare)" , change the signature of cost_compare to an appropriate one and cast the arguments inside the function, like this:
int cost_compare( void *a, void *b ){
BASKET *b1 = a;
BASKET *b2 = b;
if(b1->abs_cost < b2->abs_cost){
return 1;
}
...
else return -1;
}
and then call spec_qsort(perm + 1, basket_sizes[thread], sizeof(BASKET*), cost_compare), in this way everything is easier to read.
EDIT: To answer to one point of the last edit you made, change cost_compare to :
int cost_compare( const void *a, const void *b ){
BASKET b1 = *(BASKET *)a;
BASKET b2 = *(BASKET *)b;
if(b1.abs_cost < b2.abs_cost){
return 1;
}
...
if(*(b1.a).id > *(b2.a).id)
return 1;
else return -1;
}
Since the cost_compare function just compares abs_cost and id, why don't you pass them directly, like this.
int cost_compare(cost_t abs_cost1, int id1, cost_t abs_cost2, int id2)
{
if( abs_cost1 < abs_cost2 )
return 1;
if( abs_cost1 > abs_cost2 )
return -1;
if( id1 > id2 )
return 1;
else
return -1;
}
and then you call it like this.
const_compare(act->abs_cost, act->a->id, max->abs_cost, max->a->id)
Related
I would like to create a common structure that I may use to pass parameters of multiple lengths and types into different functions.
As an example, consider the following structure:
typedef struct _list_t {
int ID;
char *fmt;
int nparams;
} list_t;
list_t infoList[100]; //this will be pre-populated with the operations my app offers
typedef struct _common {
int ID;
char *params;
} common;
A variable size function is used to pass in the parameters given the format is already populated:
int Vfunc(common * c, ...) {
va_list args;
va_start(args, c);
//code to search for ID in infoList and fetch its fmt
char params_buff[100]; //max params is 100
vsprintf(str_params, fmt, args);
va_end(args);
c->params = (char *)malloc(sizeof(char)*(strlen(params_buff)+1));
strncpy(c->params, params_buff, strlen(params_buff)+1);
}
int execute(common * c) {
if (c->ID == 1) { //add 2 numbers
int x, y; // i expect 2 numbers
//code to find ID in infoList and fetch its fmt
sscanf(c->params, fmt, &x, &y);
return (x + y);
}
else if (c->ID == 2) {
//do another operation, i expect an unsigned char array?
}
}
Main program will look somewhat like this:
int main()
{
common c;
c.ID = 1;
Vfunc(&c, 12, 2);
execute(&c);
return 0;
}
Now I can pass in the structure to any function, which will deal with the parameters appropriately. However I do not see a way to have unsigned char[] as one of the parameters since unsigned char arrays do not have a "format". The format of a char[] would be %s. Basically I want to pass in some raw data through this structure.
Is there a way to do this or a better implementation to fulfill the goal?
EDIT:
It seems that the questions goal is unclear. Say my application can provide arithmetic operations (like a calculator). Say a user of my application wants to add 2 numbers. All I want them to do is fill in this common structure, then pass it into lets say a function to get it executed. All the ID's of the operations will be known from lets say a manual, so the user will know how many parameters they can pass and what ID does what. As the app owner i will be filling infoList with the ID's I offer.
So this is just to give you an idea of what I mean by a "common structure". It can be implemented in other ways too, maybe you have a better way. But my goal is for the implementation to be able to pass in an unsigned char array. Can I do that?
As I understand your question, you want to save all argument values in a text string so that the values can be reconstructed later using sscanf. Further, you want to be able to handle array-of-number, e.g. an array of unsigned char.
And you ask:
Is there a way to do this
For your idea to work, it's required that sscanf can parse (aka match) the type of data that you want to use in your program. And - as you write in the question - scanf can't parse arrays-of-numbers. So the answer is:
No, it can't be done with standard functions.
So if you want to be able to handle arrays-of-number, you'll have to write your own scan-function. This includes
selection of a conversion specifier to tell the code to scan for an array (e.g. %b),
select a text-format for the array (e.g. "{1, 2, 3}")
a way to store the array-data and the size, e.g. struct {unsiged char* p; size_t nb_elements;}.
Further, you'll have the same problem with vsprintf. Again you need to write your own function.
EDIT
One alternative (that I don't really like myself) is to store pointer values. That is - instead of storing the array values in the string params you can store a pointer to the array.
The upside of that approach is that you can use the standard functions.
The downside is that the caller must ensure that the array exists until execute has been called.
In other words:
unsigned char auc[] = {1, 2, 3};
Vfunc(&c, auc);
execute(&c);
would be fine but
Vfunc(&c, (unsigned char[]){1, 2, 3});
execute(&c);
would compile but fail at run time.
And - as always with arrays in C - you may need an extra argument for the number of array elements.
Example code for this "save as pointer" approach could be:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
typedef struct _common {
int ID;
char *params;
} common;
void Vfunc(common * c, ...) {
va_list args;
va_start(args, c);
//code to search for ID in infoList and fetch its fmt
// For this example just use a fixed fmt
char fmt[] ="%p %zu";
char params_buff[100]; //max params is 100
vsprintf(params_buff, fmt, args);
va_end(args);
c->params = (char *)malloc(sizeof(char)*(strlen(params_buff)+1));
strncpy(c->params, params_buff, strlen(params_buff)+1);
}
int execute(common * c)
{
if (c->ID == 1) {
// expect pointer and number of array elements
unsigned char* a;
size_t nbe;
//code to find ID in infoList and fetch its fmt
// For this example just use a fixed fmt
char fmt[] ="%p %zu";
if (sscanf(c->params, fmt, &a, &nbe) != 2) exit(1);
// Calculate average
int sum = 0;
for (size_t i = 0; i < nbe; ++i) sum += a[i];
return sum;
}
return 0;
}
int main(void)
{
common c;
c.ID = 1;
unsigned char auc[] = {1, 2, 3, 4, 5, 6};
Vfunc(&c, auc, sizeof auc / sizeof auc[0]);
printf("The saved params is \"%s\"\n", c.params);
printf("Sum of array elements are %d\n", execute(&c));
return 0;
}
Possible Output
The saved params is "0xffffcc0a 6"
Sum of array elements are 21
Notice that it's not the array data that is saved but a pointer value.
I have read the problem once again, and find out that it's much simpler than you've described.
According to your statement, you already know about the type and order of data retrieval in execute() function. This make this problem much easier.
I must say, this problem is a bit difficult to solve in c, cause c can't resolve type at runtime or dynamically cast type at runtime. c must know all the types before hand i.e. at compile time.
Now, that said, c provides a way to handle variable length arguments. And that's a advantage.
So, what we've to do is:
cache all arguments from variable length arguments i.e. va_list.
and, provide a way to retrieve provided arguments from that cache.
At first, I am going to show you how to retrieve elements from cache if you know the type. We'll do it using a macro. I've named it sarah_next(). Well, after all, I've to write it because of you. You can name it as you want. It's definition is given below:
#define sarah_next(cache, type) \
(((cache) = (cache) + sizeof(type)), \
*((type*) (char *) ((cache) - sizeof(type))))
So, in simple words, sarah_next() retrieve the next element from cache and cast it to type.
Now, let's discuss the first problem, where we've to cache all arguments from va_list. You can do it easily by writing as follows:
void *cache = malloc(sizeof(char) * cacheSize);
// itr is an iterator, which iterates over cache
char *itr = (char *)cache;
// now, you can do
*(type *)itr = va_arg(buf, type);
// and then
itr += sizeof(type);
Another, point I would like to discuss is, I've used type hint to determine cache size. For that I've used a function getSize(). You would understand if you just look at it(also note: this gives you the ability to use your own custom type):
// getSize() is a function that returns type size based on type hint
size_t getSize(char type) {
if(type == 's') {
return sizeof(char *);
}
if(type == 'c') {
return sizeof(char);
}
if(type == 'i') {
return sizeof(int);
}
if(type == 'u') { // 'u' represents 'unsigned char'
return sizeof(unsigned char);
}
if(type == 'x') { // let's, say 'x' represents 'unsigned char *'
return sizeof(unsigned char *);
}
// you can add your own custom type here
// also note: you can easily use 'unsigned char'
// use something like 'u' to represent 'unsigned char'
// and you're done
// if type is not recognized, then
printf("error: unknown type while trying to retrieve type size\n");
exit(1);
}
Ok, I guess, the ideas are complete. Before moving on try to grasp the ideas properly.
Now, let me provide the full source code:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
// note: it is the most fundamental part of this solution
// 'sarah_next' is a macro, that
// returns *(type *)buf means a value of type "type", and also
// increments 'buf' by 'sizeof(type)', so that
// it may target next element
// 'sarah_next' is used to retrieve data from task cache
// I've named it after you, you may choose to name it as you wish
#define sarah_next(cache, type) \
(((cache) = (cache) + sizeof(type)), \
*((type*) (char *) ((cache) - sizeof(type))))
// defining pool size for task pool
#define POOL_SIZE 1024
// notice: getSize() has been updated to support unsigned char and unsigned char *
// getSize() is a function that returns type size based on type hint
size_t getSize(char type) {
if(type == 's') {
return sizeof(char *);
}
if(type == 'c') {
return sizeof(char);
}
if(type == 'i') {
return sizeof(int);
}
if(type == 'u') { // 'u' represents 'unsigned char'
return sizeof(unsigned char);
}
if(type == 'x') { // let's, say 'x' represents 'unsigned char *'
return sizeof(unsigned char *);
}
// you can add your own custom type here
// also note: you can easily use 'unsigned char'
// use something like 'u' to represent 'unsigned char'
// and you're done
// if type is not recognized, then
printf("error: unknown type while trying to retrieve type size\n");
exit(1);
}
typedef struct __task {
int id;
void *cache;
} Task;
// notice: constructTask has been updated to support unsigned char and unsigned char *
// note: here, types contains type hint
Task *constructTask(int id, char *types, ...) {
// determine the size of task cache
int cacheSize = 0;
for(int i=0; types[i]; i++) {
cacheSize += getSize(types[i]);
}
// allocate memory for task cache
void *cache = malloc(sizeof(char) * cacheSize);
va_list buf;
va_start(buf, types);
// itr is an iterator, which iterates over cache
char *itr = (char *)cache;
for(int i=0; types[i]; i++) {
if(types[i] == 's') {
*(char **)itr = va_arg(buf, char *);
} else if(types[i] == 'x') { // added support for 'unsigned char *'
*(unsigned char **)itr = va_arg(buf, unsigned char *);
} else if(types[i] == 'c') {
// notice: i used 'int' not 'char'
// cause: compiler-warning: 'char' is promoted to 'int' when passed through '...'
// also note: this promotion helps with 'unsigned char'
*(char *)itr = (char)va_arg(buf, int); // so cast it to char
} else if(types[i] == 'u') { // added support 'unsigned char'
// notice: i used 'int' not 'unsigned char'
// cause: compiler-warning: 'unsigned char' is promoted to 'int' when passed through '...'
// also note: this promotion helps with 'unsigned char'
*(unsigned char *)itr = (unsigned char)va_arg(buf, int); // so cast it to unsigned char
} else if(types[i] == 'i') {
*(int *)itr = va_arg(buf, int);
}
// it won't come to else, cause getSize() would
// caught the type error first and exit the program
itr += getSize(types[i]);
}
va_end(buf);
// now, construct task
Task *task = malloc(sizeof(Task));
task->id = id;
task->cache = cache;
// and return it
return task;
}
// destroyTask is a function that frees memory of task cache and task
void destroyTask(Task *task) {
free(task->cache);
free(task);
}
// notice: that 'task->id == 4' processing part
// it is equivalant to your 'execute()' function
int taskProcessor(Task *task) {
// define ret i.e. return value
int ret = 999; // by default it is some code value, that says error
// note: you already know, what type is required in a task
if(task->id == 1) {
// note: see usage of 'sarah_next()'
int x = sarah_next(task->cache, int);
int y = sarah_next(task->cache, int);
ret = x + y;
} else if(task->id == 2) {
char *name = sarah_next(task->cache, char *);
if(strcmp(name, "sarah") == 0) {
ret = 0; // first name
} else if (strcmp(name, "cartenz") == 0) {
ret = 1; // last name
} else {
ret = -1; // name not matched
}
} else if(task->id == 3) {
int x = sarah_next(task->cache, int);
char *name = sarah_next(task->cache, char *);
int y = sarah_next(task->cache, int);
printf("%d %s %d\n", x, name, y); // notice: we've been able to retrieve
// both string(i.e. char *) and int
// you can also see for ch and int, but i can assure you, it works
ret = x + y;
} else if(task->id == 4) { // working with 'unsigned char *'
int a = sarah_next(task->cache, int);
unsigned char *x = sarah_next(task->cache, unsigned char *); // cast to unsigned char *
// char *x = sarah_next(task->cache, char *); // this won't work, would give wrong result
int b = sarah_next(task->cache, int);
printf("working with 'unsigned char *':");
for(int i=0; x[i]; i++) {
printf(" %d", x[i]); // checking if proper value is returned, that's why using 'integer'
}
printf("\n");
ret = a + b;
} else {
printf("task id not recognized\n");
}
return ret;
}
int main() {
Task *taskPool[POOL_SIZE];
int taskCnt = 0;
taskPool[taskCnt++] = constructTask(1, "ii", 20, 30); // it would return 50
taskPool[taskCnt++] = constructTask(1, "ii", 50, 70); // it would return 120
taskPool[taskCnt++] = constructTask(2, "s", "sarah"); // it would return 0
taskPool[taskCnt++] = constructTask(2, "s", "cartenz"); // it would return 1
taskPool[taskCnt++] = constructTask(2, "s", "reyad"); // it would return -1
taskPool[taskCnt++] = constructTask(3, "isi", 40, "sarah", 60); // it would print [40 sarah 60] and return 100
// notice: I've added an exmaple to showcase the use of unsigned char *
// also notice: i'm using value greater than 127, cause
// in most compiler(those treat char as signed) char supports only upto 127
unsigned char x[] = {231, 245, 120, 255, 0}; // 0 is for passing 'NULL CHAR' at the end of string
// 'x' is used to represent 'unsigned char *'
taskPool[taskCnt++] = constructTask(4, "ixi", 33, x, 789); // it would print ['working with unsigned char *': 231 245 120 255] and return 822
// note: if you used 'char *' cast to retrieve from 'cache'(using a compiler which treats char as signed), then
// it would print [-25 -11 120 -1] instead of [231 245 120 255]
// i guess, that makes it clear that you can perfectly use 'unsigned char *'
for(int i=0; i<taskCnt; i++) {
printf("task(%d): %d\n", i+1, taskProcessor(taskPool[i]));
printf("\n");
}
// at last destroy all tasks
for(int i=0; i<taskCnt; i++) {
destroyTask(taskPool[i]);
}
return 0;
}
The output is:
// notice the updated output
task(1): 50
task(2): 120
task(3): 0
task(4): 1
task(5): -1
40 sarah 60
task(6): 100
working with 'unsigned char *': 231 245 120 255
task(7): 822
So, you may be wondering, what advantage it may create over your given solution. Well, first of all you don't have to use %s %d etc. do determine format, which is not easy to change or create for each task and you've write a lot may be(for each task, you may have to write different fmt), and you don't have use vsprintf etc... which only deals with builtin types.
And the second and great point is, you can use your own custom type. Declare a struct type of your own and you can use. And it is also easy to add new type.
update:
I've forgot to mention another advantage, you can also use unsigned char with it. see, the updated getSize() function. you can use 'u' symbol for unsigned char, and as unsigned char is promoted to int, you can just cast it to (unsigned char) and done...
update-2(support for unsigned char *):
I have updated the code to support unsigned char and unsigned char *. To support new type, the functions, those you need to update, are getSize() and constructTask(). Compare the previous code and the newly updated code...you'll understand how to add new types(you can also add custom types of your own).
Also, take a look at task->id == 4 part in taskProcessor() function. I've added this to showcase the usage of unsigned char *. Hope this clears everything.
If you've any question, then ask me in the comment...
I think what you want from your description is likely a union of structs, with the 1st member of the union being an enumerator that defines the type of the structure in use, which is how polymorphism is often accomplished in C. Look at the X11 headers for a giant example of Xevents
A trivial example:
//define our types
typedef enum {
chicken,
cow,
no_animals_defined
} animal;
typedef struct {
animal species;
int foo;
char bar[20];
} s_chicken;
typedef struct {
animal species;
double foo;
double chew;
char bar[20];
} s_cow;
typedef union {
animal species; // we need this so the receiving function can identify the type.
s_chicken chicken ;
s_cow cow ;
} s_any_species;
now, this struct may be passed to a function and take on either identity. A receiving function of a type s_any_species may do to de-reference.
void myfunc (s_any_species any_species)
{
if (any_species.species == chicken)
any_species.chicken.foo=1 ;
}
Arrays of function pointers are preferable to long if else sequences here, but either will work
I take you to be asking about conveying a sequence of objects a varying type to a function. As a special detail, you want the function to receive only one actual argument, but this is not particularly significant because it is always possible to convert a function that takes multiple arguments into another that takes only one by wrapping the multiple parameters in a corresponding structure. I furthermore take the example Vfunc() code's usage of vsprintf() to be an implementation detail, as opposed to an essential component of the required solution.
In that case, notwithstanding my grave doubts about the usefulness of what you seem to want, as a C programming exercise it does not appear to be that difficult. The basic idea you seem to be looking for is called a tagged union. It goes by other names, too, but that one matches up well with the relevant C-language concepts and keywords. The central idea is that you define a type that can hold objects of various other types, one at a time, and that carries an additional member that identifies which type each instance currently holds.
For example:
enum tag { TAG_INT, TAG_DOUBLE, TAG_CHAR_PTR };
union tagged {
struct {
enum tag tag;
// no data -- this explicitly gives generic access to the tag
} as_any;
struct {
enum tag tag;
int data;
} as_int;
struct {
enum tag tag;
double data;
} as_double;
struct {
enum tag tag;
char *data;
} as_char_ptr;
// etc.
};
You could then combine that with a simple list wrapper:
struct arg_list {
unsigned num;
union tagged *args;
};
Then, given a function such as this:
int foo(char *s, double d) {
char[16] buffer;
sprintf(buffer, "%15.7e", d);
return strcmp(s, buffer);
}
You might then wrap it like so:
union tagged foo_wrapper(struct arg_list args) {
// ... validate argument count and types ...
return (union tagged) { .as_int = {
.tag = TAG_INT, .data = foo(args[0].as_char_ptr.data, args[1].as_double.data)
} };
}
and call the wrapper like so:
void demo_foo_wrapper() {
union tagged arg_unions[2] = {
{ .as_char_ptr = { .tag = TAG_CHAR_PTR, .data = "0.0000000e+00" },
{ .as_double = { .tag = TAG_DOUBLE, .data = 0.0 }
};
union tagged result = foo_wrapper((struct arg_list) { .num = 2, .args = arg_unions});
printf("result: %d\n", result.as_int.data);
}
Update:
I suggested tagged unions because the tags correspond to the field directives in the format strings described in the question, but if they aren't useful to you in practice then they are not an essential detail of this approach. If the called functions will work on the assumption that the caller has packed arguments correctly, and you have no other use for tagging the data with their types, then you can substitute a simpler, plain union for a tagged union:
union varying {
int as_int;
double as_double;
char *as_char_ptr;
// etc.
};
struct arg_list {
unsigned num;
union varying *args;
};
union varying foo_wrapper(struct arg_list args) {
return (union vaying) { .as_int = foo(args[0].as_char_ptr, args[1].as_double) };
}
void demo_foo_wrapper() {
union varying arg_unions[2] = {
.as_char_ptr = "0.0000000e+00",
.as_double = 0.0
};
union varying result = foo_wrapper((struct arg_list) { .num = 2, .args = arg_unions});
printf("result: %d\n", result.as_int);
}
I am new to C and wondering how to do some pointer stuff. Specifically here I am wondering how you can pass a pointer into a function and "get a value out of the function". Sort of like this (semi-pseudocode):
assign_value_to_pointer(void* pointer) {
if (cond1) {
pointer = 10;
} else if (cond2) {
pointer = "foo";
} else if (cond3) {
pointer = true;
} else if (cond4) {
pointer = somestruct;
} else if (cond5) {
pointer = NULL;
} else if (cond6) {
// unknown type!
pointer = flexiblearraymember.items[index];
}
}
main() {
void* pointer = NULL;
assign_value_to_pointer(&pointer);
if (cond1) {
assert(pointer == 10);
} else if (cond2) {
assert(pointer == "foo");
} else if (cond3) {
assert(pointer == true);
} else if (cond4) {
assert(pointer == somestruct);
} else if (cond5) {
assert(pointer == NULL);
}
}
Put another way:
p = new Pointer()
assign_a_value(p)
assert(p.value == 10) // or whatever
Basically it is passing the pointer into the function, the function is assigning a value to the pointer, and then you can use that value outside of the function when it returns. You may not know what kind of value you are getting from the function (but that can be handled by extending this to use structs and such), hence the void pointer. The main goal though is just passing a pointer into some function and having it absorb some value.
Wondering how to do this properly in C with a quick example implementation. Doesn't have to cover every case just enough to get started.
I would like to use this to implement stuff like passing in a NULL error object to a function, and if there is an error, it sets the pointer of the error to some error code, etc.
I don't think this should be a broad question, but if it is, it would be helpful to know where to look for a more thorough explanation or examples in source code.
First, I'll answer your question directly, hopefully you understand why you need to be reaaally careful. This can be a useful technique for implementing queues, or communication stacks - but you need to be CERTAIN that you can regain track of what types are being stored or your program logic will totally break. I'll then try to briefly cover some of the use cases and some methods of making it safe(r).
Simple example doing exactly what you said
#include <stdio.h>
#include <stdlib.h>
//Some basic error type for reporting failures
typedef enum my_error
{
ERROR_NONE = 0,
ERROR_FAIL = 1,
} my_error;
struct my_struct
{
int age;
char *name;
int order_count;
};
int someCond = 1;
//Let's start with a simple case, where we know the type of the pointer being passed (an int)
//If int_out is NULL, then this function will invoke undefined behavior (probably a
//runtime crash, but don't rely on it).
my_error assign_int(int *int_out)
{
if(someCond)
*int_out = 5;
else
*int_out = 38;
return ERROR_NONE;
}
//Need to use a 'double pointer', so that this function is actually changing the pointer
//that exists in the parent scope
my_error dynamically_assign_value_to_pointer(void **pointer)
{
//A pointer internal to this function just to simplify syntax
void *working_ptr = NULL;
if(someCond)
{
//Allocate a region of memory, and store its location in working_ptr
working_ptr = malloc(sizeof(int));
//store the value 12 at the location that working_ptr points to (using '*' to dereference)
*((int *) working_ptr) = 12;
}
else
{
//Allocate a region of memory, and store its location in working_ptr
working_ptr = malloc(sizeof(struct my_struct));
//Fill the struct with data by casting (You can't dereference a void pointer,
//as the compiler doesn't know what it is.)
((struct my_struct *) working_ptr)->age = 22;
((struct my_struct *) working_ptr)->name = "Peter";
((struct my_struct *) working_ptr)->order_count = 6;
}
//Set the pointer passed as an argument to point to this data, by setting the
//once-dereferenced value
*pointer = working_ptr;
return ERROR_NONE;
}
int main (int argc, char *argv[])
{
int an_int;
void *some_data;
assign_int(&an_int);
//an_int is now either 5 or 38
dynamically_assign_value_to_pointer(&some_data);
//some_data now points to either an integer OR a my_struct instance. You will need
//some way to track this, otherwise the data is useless.
//If you get this wrong, the data will be interpreted as the wrong type, and the
//severity of the issue depends what you do with it.
//For instance, if you KNOW FOR SURE that the pointer contains the int, you could
//print it by:
printf("%d", *((int *) some_data));
//And because it is dynamically allocated, you MUST free it.
free(some_data);
return 0;
}
In practice, this is useful for queues, for instance, so you can write a generic queue function and then have different queues for different data types. This is partial code, so won't compile and is a bad idea in this limited case, when a type-safe alternative would be trivial to design, but hopefully you get the idea:
extern my_queue_type myIntQueue;
extern my_queue_type myStructQueue;
my_error get_from_queue(void *data_out, my_queue_type queue_in);
int main (int argc, char *argv[])
{
//...
int current_int;
struct my_struct current_struct;
get_from_queue(¤t_int, myIntQueue);
get_from_queue(¤t_struct, myStructQueue);
//...
}
Or if you really want to store lots of different types together, you should at least track the type along with the pointer in a struct, so you can use a 'switch' in order to cast and handle logic appropriately when necessary. Again, partial example so won't compile.
enum my_types
{
MY_INTEGER, MY_DOUBLE, MY_STRUCT
};
struct my_typed_void
{
void *data;
enum my_types datatype;
};
my_error get_dynamic_from_global_queue(struct my_typed_void *data_out)
{
//...
data_out->data = malloc(sizeof int);
*((int *)(data_out->data)) = 33;
data_out->datatype = MY_INTEGER;
//...
}
int main (int argc, char *argv[])
{
struct my_typed_void current;
if(get_dynamic_from_global_queue(¤t) == ERROR_NONE)
{
switch(current.datatype)
{
//...
case MY_INTEGER:
printf("%d", *((int *) current.data));
break;
//...
}
free(current.data);
}
return 0;
}
Either return the pointer or pass a pointer to pointer (the function then will change the pointer):
void* f1(void* p)
{
p = whatever(p, conditions);
return p;
}
void f2(void** p)
{
*p = whatever(*p, conditions);
}
void assign_value_to_pointer(int** pointer) {
**pointer = 20;
}
void main() {
void* pointer = NULL;
pointer=malloc(sizeof(int));
*(int *)pointer=10;
assign_value_to_pointer(&pointer);
}
I'm not 100% sure what you are looking for, but could it be something like this:
enum pointer_type{INTEGER, STRUCTURE_1, STRUCTURE_2, INVALID};
int assign_value_to_pointer(void ** ptr)
{
uint8_t cond = getCondition();
switch(cond)
{
case 1:
*ptr = (void*) 10;
return INTEGER;
case 2:
*ptr = (void*) someStructOfType1;
return STRUCTURE_1;
case 3:
*ptr = (void*) someStructOfType2;
return STRUCTURE_2;
default:
*ptr = NULL;
return INVALID;
};
}
void main(void)
{
void * ptr = NULL;
int ptrType = assign_value_to_pointer(&ptr);
switch(ptrType)
{
case INTEGER:
assert(ptr == (void*)10);
break;
case STRUCTURE_1:
assert( ((structType1*) ptr)->thing == something);
break;
case STRUCTURE_2:
assert( ((structType2*) ptr)->something == something);
break;
default:
assert(ptr == NULL);
}
}
You can actually type cast the pointer in main() according to the case (condition) and use. However, in my opinion, you can use a union for this purpose.
Create a union with all possible data types.
typedef union _my_union_type_ {
int intVal;
char* stringVal;
bool boolVal;
SomestructType somestruct;//Assuming you need a structure not structure pointer.
void* voidPtrType;
} my_union_type;
Now in main(), create variable of this union type and pass the address of the union to the function.
main() {
my_union_type my_union;
memset(&my_union, 0x00, sizeof(my_union));
assign_value_to_pointer(&my_union);
if (cond1) {
assert(my_union.intVal == 10);
} else if (cond2) {
assert(strcmp(my_union.stringVal, "foo")); //String comparison can not be done using '=='
} else if (cond3) {
assert(my_union.boolVal == true);
} else if (cond4) {
assert(memcmp(&my_union.somestruct, &somestruct, sizeof(somestruct)); //Assuming structure not structure pointer.
} else if (cond5) {
assert(my_union.voidPtrType == NULL);
} else if (cond5) {
//Check my_union.voidPtrType
}
}
And in assign_value_to_pointer, you can store the required value in union variable.
assign_value_to_pointer(my_union_type* my_union) {
if (cond1) {
my_union->intVal = 10;
} else if (cond2) {
my_union->stringVal = "foo";
} else if (cond3) {
my_union->boolVal = true;
} else if (cond4) {
memcpy(&(my_union->somestruct), &somestruct, sizeof(somestruct));
} else if (cond5) {
my_union->voidPtrType = NULL;
} else if (cond6) {
// unknown type!
my_union->voidPtrType = flexiblearraymember.items[index];
}
}
I would like to use this to implement stuff like passing in a NULL error object to a function, and if there is an error, it sets the pointer of the error to some error code, etc.
From the above quote and from the code in the question, it seems you are looking for a variable that can "hold" different types, i.e. sometimes you want it to be an integer, at other times a float, at other times a string and so on. This is called a variant in some languages but variants doesn't exist in C. (see this https://en.wikipedia.org/wiki/Variant_type for more about variants)
So in C you'll have to code your own variant type. There are several ways to do that. I'll give examples below.
But first a few words on pointers in C because the code in the question seem to reveal a misunderstanding as it assigns values directly to the pointer, e.g. pointer = somestruct; which is illegal.
In C is very important to understand the difference between the "value of a pointer" and the "value of the pointed to object". The first, i.e. value of a pointer, tells where the pointer is pointing, i.e. the value of a pointer is the address of the pointed to object. Assignments to a pointer changes where the pointer is pointing. To change the value of the pointed to object, the pointer must be dereferenced first. Example (pseudo code):
pointer = &some_int; // Make pointer point to some_int
*pointer = 10; // Change the value of the pointed to object, i.e. some_int
// Notice the * in front of pointer - it's the dereference
// that tells you want to operate on the "pointed to object"
pointer = 10; // Change the value of the pointer, i.e. where it points to
// In other words, pointer no longer points to some_int
Now back to the "variant" implementation. As already mentioned there are several ways to code that in C.
From your question it seems that you want to use a void-pointer. It's doable and I'll start by showing an example using void-pointer and after that an example using a union.
It's not clear in your question what cond are so in my examples I'll just assume it's a command line argument and I just added some interpretation in order to have a running example.
The common pattern for the examples is the use of a "tag". That is an extra variable that tells the current type of objects value (aka meta-data). So the general variant data type looks like:
struct my_variant
{
TagType tag; // Tells the current type of the value object
ValueType value; // The actual value. ValueType is a type that allows
// storing different object types, e.g. a void-pointer or a union
}
Example 1 : void-pointer and casts
The example below will use a void-pointer to point to the object containing the real value. A value that sometimes is an integer, sometimes a float or whatever is needed. When working with a void-pointer, it's necessary to cast the void-pointer before dereferencing the pointer (i.e. before accessing the pointed to object). The tag field tells the type of the pointed to object and thereby also how the cast shall be.
#include <stdio.h>
#include <stdlib.h>
// This is the TAG type.
// To keep the example short it only has int and float but more can
// be added using the same pattern
typedef enum
{
INT_ERROR_TYPE,
FLOAT_ERROR_TYPE,
UNKNOWN_ERROR_TYPE,
} error_type_e;
// This is the variant type
typedef struct
{
error_type_e tag; // The tag tells the type of the object pointed to by value_ptr
void* value_ptr; // void pointer to error value
} error_object_t;
// This function evaluates the error and (if needed)
// creates an error object (i.e. the variant) and
// assigns appropriate values of different types
error_object_t* get_error_object(int err)
{
if (err >= 0)
{
// No error
return NULL;
}
// Allocate the variant
error_object_t* result_ptr = malloc(sizeof *result_ptr);
// Set tag value
// Allocate value object
// Set value of value object
if (err > -100) // -99 .. -1 is INT error type
{
result_ptr->tag = INT_ERROR_TYPE;
result_ptr->value_ptr = malloc(sizeof(int));
*(int*)result_ptr->value_ptr = 42;
}
else if (err > -200) // -199 .. -100 is FLOAT error type
{
result_ptr->tag = FLOAT_ERROR_TYPE;
result_ptr->value_ptr = malloc(sizeof(float));
*(float*)result_ptr->value_ptr = 42.42;
}
else
{
result_ptr->tag = UNKNOWN_ERROR_TYPE;
result_ptr->value_ptr = NULL;
}
return result_ptr;
}
int main(int argc, char* argv[])
{
if (argc < 2) {printf("Missing arg\n"); exit(1);}
int err = atoi(argv[1]); // Convert cmd line arg to int
error_object_t* err_ptr = get_error_object(err);
if (err_ptr == NULL)
{
// No error
// ... add "normal" code here - for now just print a message
printf("No error\n");
}
else
{
// Error
// ... add error handler here - for now just print a message
switch(err_ptr->tag)
{
case INT_ERROR_TYPE:
printf("Error type INT, value %d\n", *(int*)err_ptr->value_ptr);
break;
case FLOAT_ERROR_TYPE:
printf("Error type FLOAT, value %f\n", *(float*)err_ptr->value_ptr);
break;
default:
printf("Error type UNKNOWN, no value to print\n");
break;
}
free(err_ptr->value_ptr);
free(err_ptr);
}
return 0;
}
Some examples of running this program:
> ./prog 5
No error
> ./prog -5
Error type INT, value 42
> ./prog -105
Error type FLOAT, value 42.419998
> ./prog -205
Error type UNKNOWN, no value to print
As the example above shows, you can implement a variant type using void-pointer. However, the code requires a lot of casting which makes the code hard to read. In general I'll not recommend this approach unless you have some special requirements that forces the use of void-pointer.
Example 2 : pointer to union
As explained earlier C doesn't have variants as they are known in other languages. However, C has something that is pretty close. That is unions. A union can hold different types at different times - all it misses is a tag. So instead of using a tag and a void-pointer, you can use a tag and a union. The benefit is that 1) casting will not be needed and 2) a malloc is avoided. Example:
#include <stdio.h>
#include <stdlib.h>
typedef enum
{
INT_ERROR_TYPE,
FLOAT_ERROR_TYPE,
UNKNOWN_ERROR_TYPE,
} error_type_e;
// The union that can hold an int or a float as needed
typedef union
{
int n;
float f;
} error_union_t;
typedef struct
{
error_type_e tag; // The tag tells the current union use
error_union_t value; // Union of error values
} error_object_t;
error_object_t* get_error_object(int err)
{
if (err >= 0)
{
// No error
return NULL;
}
error_object_t* result_ptr = malloc(sizeof *result_ptr);
if (err > -100) // -99 .. -1 is INT error type
{
result_ptr->tag = INT_ERROR_TYPE;
result_ptr->value.n = 42;
}
else if (err > -200) // -199 .. -100 is FLOAT error type
{
result_ptr->tag = FLOAT_ERROR_TYPE;
result_ptr->value.f = 42.42;
}
else
{
result_ptr->tag = UNKNOWN_ERROR_TYPE;
}
return result_ptr;
}
int main(int argc, char* argv[])
{
if (argc < 2) {printf("Missing arg\n"); exit(1);}
int err = atoi(argv[1]); // Convert cmd line arg to int
error_object_t* err_ptr = get_error_object(err);
if (err_ptr == NULL)
{
// No error
// ... add "normal" code here - for now just print a message
printf("No error\n");
}
else
{
// Error
// ... add error handler here - for now just print a message
switch(err_ptr->tag)
{
case INT_ERROR_TYPE:
printf("Error type INT, value %d\n", err_ptr->value.n);
break;
case FLOAT_ERROR_TYPE:
printf("Error type FLOAT, value %f\n", err_ptr->value.f);
break;
default:
printf("Error type UNKNOWN, no value to print\n");
break;
}
free(err_ptr);
}
return 0;
}
In my opinion this code is easier to read than the code using void-pointer.
Example 3 : union - no pointer - no malloc
Even if example 2 is better than example 1 there is still dynamic memory allocation in example 2. Dynamic allocation is part of most C programs but it is something that shall be used only when really needed. In other words - objects with automatic storage duration (aka local variables) shall be prefered over dynamic allocated objects when possible.
The example below shows how the dynamic allocation can be avoided.
#include <stdio.h>
#include <stdlib.h>
typedef enum
{
NO_ERROR,
INT_ERROR_TYPE,
FLOAT_ERROR_TYPE,
UNKNOWN_ERROR_TYPE,
} error_type_e;
typedef union
{
int n;
float f;
} error_union_t;
typedef struct
{
error_type_e tag; // The tag tells the current union usevalue_ptr
error_union_t value; // Union of error values
} error_object_t;
error_object_t get_error_object(int err)
{
error_object_t result_obj;
if (err >= 0)
{
// No error
result_obj.tag = NO_ERROR;
}
else if (err > -100) // -99 .. -1 is INT error type
{
result_obj.tag = INT_ERROR_TYPE;
result_obj.value.n = 42;
}
else if (err > -200) // -199 .. -100 is FLOAT error type
{
result_obj.tag = FLOAT_ERROR_TYPE;
result_obj.value.f = 42.42;
}
else
{
result_obj.tag = UNKNOWN_ERROR_TYPE;
}
return result_obj;
}
int main(int argc, char* argv[])
{
if (argc < 2) {printf("Missing arg\n"); exit(1);}
int err = atoi(argv[1]); // Convert cmd line arg to int
error_object_t err_obj = get_error_object(err);
switch(err_obj.tag)
{
case NO_ERROR:
printf("No error\n");
break;
case INT_ERROR_TYPE:
printf("Error type INT, value %d\n", err_obj.value.n);
break;
case FLOAT_ERROR_TYPE:
printf("Error type FLOAT, value %f\n", err_obj.value.f);
break;
default:
printf("Error type UNKNOWN, no value to print\n");
break;
}
return 0;
}
Summary
There are many ways of solving the problem addressed by OP. Three examples have been given in this answer. In my opinion example 3 is the best approach as it avoids dynamic memory allocation and pointers but there may be situations where example 1 or 2 is better.
You are not far from success, you just miss an asterisk to dereference the argument:
void assign_value_to_pointer(void* pointer) {
if (cond1) {
*pointer = 10; // note the asterisk
...
}
void main() {
void* pointer = NULL;
assign_value_to_pointer(&pointer);
}
In C language, arguments to functions are always passed by value. If you want the function to modify the argument, you must pass the address of the variable you want to modify. In main(), you are doing that - correct. The called function can write where its argument points to, hence modifying the original variable; to do this, you must dereference the argument.
The compiler should get angry on the assignment, because it does not know how many bytes to write (I'm keeping it simple). So, you have to say what kind of object the pointer points to, like this:
*(int *) pointer = 10;
The typecast you choose is up to you, it depends on the context.
At this point... why not declare differently the function:
void assign_value_to_pointer(int* pointer) {
if (cond1) {
*pointer = 10; // note the asterisk
}
Now the typecast is no more necessary because the compiler knows the kind of object (again I am keeping it simple - void is quite special).
******* EDIT after comments
Well, I am not a guru in C language and, besides, I wanted to keep a low profile to better help the OP.
For simple cases, the right declaration is naive. The typecast can be more flexible because the function can have several assignment statements to choose from depending on context. Lastly, if the function is passed the pointer and some other parameter, everything is possible, including using memcpy(). But this last solution opens up a world...
To reply to Lance (comment below): well, I think that there is no way to do an assignment if you don't know the type of object you are writing to. It seems a contracdition to me...
So I´m stuck with this sort function because everything seems to work fine when I debug it and there are no errors or warnings what so ever but it somehow gets stuck in an infinite loop.
My struct(if it helps):
typedef struct raeume{
char number[5];
char klasse[6];
int tische;
}raeume;
my start of the qsort function:
void ausgabesortiert(struct raeume *arr[],int used,int size)
{
qsort(*arr,size,sizeof(raeume),cmp);
ausgabesortiert(arr,size,used);
}
my compare function:
int cmp(const void * a, const void * b)
{
raeume *raumA = (raeume *) a;
raeume *raumB = (raeume *) b;
int tempA = raumA->klasse[0] - '0';
int tempB = raumB->klasse[0] - '0';
if(tempA < tempB)
{
return -1;
}
else if(tempA > tempB)
{
return 1;
}
else if(tempA == tempB)
{
if(raumA->tische > raumB->tische)
{
return -1;
}
else if(raumA->tische < raumB->tische)
{
return 1;
}
else if(raumA->tische == raumB->tische)
{
return 0;
}
}
return 0;
}
The declaration of your ausgabesortiert function
void ausgabesortiert(struct raeume *arr[],int used,int size)
clearly suggests that array arr contains pointers to struct raeume objects, not the objects themselves.
But the call to qsort
qsort(*arr,size,sizeof(raeume),cmp);
and the comparison function are written as if you are trying to sort an array of struct raeume objects themselves that begins at arr[0] location.
While there's nothing formally invalid in this, it still looks rather strange. Is this really your intent? What exactly are you trying to sort, again? The arr array or some other array pointed by arr[0]? I suspect that it is the former, in which case you need to fix the qsort call and comparison function.
const static int VECTOR_BASIC_LENGTH = 20;
struct m_vector
{
void* my_vector;
size_t my_capacity;
size_t my_head;
};
typedef struct m_vector Vector;
Vector creat_Vector(size_t size,void *judge)
{
Vector _vector;
size = size?size:VECTOR_BASIC_LENGTH;
_vector.my_capacity = size;
_vector.my_head = 0;
//How I write the following two lines
_vector.my_vector = malloc(sizeof(*judge) * size);
return _vector;
}
The type of judge is uncertain,so I pass a void pointer as a parameters.I need the size of *judge to allocate memory to _vector.my_vector,for example if I use:
int *a;
creat_Vector(5,a);
I want the following line:
_vector.my_vector = malloc(sizeof(*judge)*size);
is equal to:
_vector.my_vector = malloc(sizeof(*a)*5);
How could I achieve this function.Using pure C
There is a forbidden thing done in your code.
You statically (at compile time) allocate/declare a local _vector of type Vector in your function creat_Vector. Then you return this object to the outside world. However, when you are exiting your function, all local data is dead. So, you should absolutely rethink this.
One suggestion would be:
int init_Vector(Vector* _vect, size_t size, unsigned int ptr_size)
{
size = size?size:VECTOR_BASIC_LENGTH;
_vect->my_capacity = size;
_vect->my_head = 0;
_vect->my_vector = malloc(size*ptr_size);
if (_vect->my_vector) {
return 0;
}
return 1;
}
Then:
Vector _vector;
char *a;
if (init_Vector(&_vector, 5, sizeof(char)) == 0) {
printf("Success!\n");
}
else {
printf("Failure!\n");
/* treat appropriately (return error code/exit) */
}
/* do whatever with a (if needed) and _vector*/
I try to sort a struct below, given an intention to sort their error rate, while retaining the information of sid and did. While there is no compilation error, I get a seg fault in runtime. I wonder what has gone wrong....
#include <stdio.h>
#include <stdlib.h>
struct linkdata {
int sid;
int did;
double err;
};
typedef struct linkdata LD;
typedef int (*qsort_func_t)(const void *, const void *);
static int compareByErr (const void * a, const void * b)
{
fprintf(stderr, "aerr=%.3f, berr=%.3f\n", (*(LD**)a)->err, (*(LD**)b)->err);
int aerr = (*(LD**)a)->err;
int berr = (*(LD**)b)->err;
return aerr - berr;
}
int main() {
int idx;
int numnode;
struct linkdata* perr;
qsort_func_t qsort_func = compareByErr;
numnode = 3;
perr = (LD*) malloc (numnode*numnode*sizeof(LD));
perr[0].sid = 0; perr[0].did = 1; perr[0].err = 0.642;
perr[1].sid = 0; perr[1].did = 2; perr[1].err = 0.236;
perr[2].sid = 0; perr[2].did = 3; perr[2].err = 0.946;
idx = 3;
qsort(perr, idx, sizeof(perr), compareByErr);
int i;
for (i=0; i<idx; i++){
fprintf(stderr,"err[%d][%d] = %.3f\n", perr[i].sid, perr[i].did, perr[i].err);
}
free(perr);
}
There are many errors in the code.
1. compareByErr
The a and b parameters of the compareByErr function are objects of LD*, not LD**. You did an unnecessary dereferencing. Try to change that function to:
static int compareByErr (const void * a, const void * b)
{
fprintf(stderr, "aerr=%.3f, berr=%.3f\n", ((LD*)a)->err, ((LD*)b)->err);
int aerr = ((LD*)a)->err;
int berr = ((LD*)b)->err;
return aerr - berr;
}
2. compareByErr
There is another problem, that you implicitly convert the double into int. Since all those "errors" are 0.???, they will all be truncated to 0. Making the whole array unsorted. Change it to:
double aerr = ((LD*)a)->err;
double berr = ((LD*)b)->err;
return aerr < berr ? -1 : aerr > berr ? 1 : 0;
3. malloc
You are allocating for 32 nodes, but only 3 are needed. Change that to
perr = (LD*) malloc (numnode * sizeof(LD));
4. qsort
The 3rd argument is the size of each element of the array, not sizeof(perr) which is just the size of a pointer (4 bytes). Change that line to:
qsort(perr, idx, sizeof(*perr), compareByErr);
// ^
to actually get the element size.
The idx seems unnecessary. You could just use numnode here.
Your comparison function expects to be sorting an array of pointers to structs, but you're not doing that. This problem is covered by the other answers.
What they didn't mention is that you're also using the wrong sizeof for the sort. Since the array is an array of structs, you must tell qsort that the size of a member is the size of a struct. Change sizeof perr to sizeof *perr
Also, converting the floats to ints before comparing them results in them all being equal because they're all zero...
You're mis-treating the arguments to your comparator callback.
This:
fprintf(stderr, "aerr=%.3f, berr=%.3f\n", (*(LD**)a)->err, (*(LD**)b)->err);
should be:
{
const LD *lda = a, *ldb = b;
fprintf(stderr, "aerr=%.3f, berr=%.3f\n", lda->err, ldb->err);
/* ... */
}
Of course you don't have to introduce new variables of the proper type, but it makes the subsequent code that much easier. I always do this.
Further, this:
int aerr = (*(LD**)a)->err;
int berr = (*(LD**)b)->err;
return aerr - berr;
is adoringly terse, but it can hide integer overflow issues that are bit scary. I would recommend:
return (a->err < b->err) ? -1 : a->err > b->err;
This uses an explicit literal to generate the -1 value, while relying on comparisons generating 0 or 1 for the two other cases.