M
MC Andre
I'm writing a port of QuickCheck in C. Working code is available at
GitHub (https://github.com/mcandre/qc). There are several problems to
consider, but I'll limit myself to one for now.
There's a gen_array() function that should populate an array of random
length (e.g. { 1, 5, 7 } or "ArF"), using a generator function to fill
in values. It has the following signature:
typedef void (*fp)();
void* gen_array(fp gen, size_t size);
Where gen is a nullary function that returns a value, for example:
char gen_char() {
return (char) (rand() % 128);
}
To create a random string, one would write:
char* random_string = (char*) gen_array(gen_char, sizeof(char));
For now, I'm getting a compiler error.
$ make
gcc -o example example.c qc.c qc.h -lgc
qc.c: In function ‘gen_array’:
qc.c:29: warning: dereferencing ‘void *’ pointer
qc.c:29: error: invalid use of void expression
make: *** [example] Error 1
A StackOverflow user has posted code that almost works. It compiles,
but it requires generator functions to do pointer arithmetic,
something I don't want users of my framework to have to do.
http://stackoverflow.com/questions/...on-passed-to-another-function/7523699#7523699
If anyone can provide a version of gen_array that doesn't require
passing values to generators, and doesn't require globals, I'd be
elated to see it.
Cheers,
Andrew Pennebaker
GitHub (https://github.com/mcandre/qc). There are several problems to
consider, but I'll limit myself to one for now.
There's a gen_array() function that should populate an array of random
length (e.g. { 1, 5, 7 } or "ArF"), using a generator function to fill
in values. It has the following signature:
typedef void (*fp)();
void* gen_array(fp gen, size_t size);
Where gen is a nullary function that returns a value, for example:
char gen_char() {
return (char) (rand() % 128);
}
To create a random string, one would write:
char* random_string = (char*) gen_array(gen_char, sizeof(char));
For now, I'm getting a compiler error.
$ make
gcc -o example example.c qc.c qc.h -lgc
qc.c: In function ‘gen_array’:
qc.c:29: warning: dereferencing ‘void *’ pointer
qc.c:29: error: invalid use of void expression
make: *** [example] Error 1
A StackOverflow user has posted code that almost works. It compiles,
but it requires generator functions to do pointer arithmetic,
something I don't want users of my framework to have to do.
http://stackoverflow.com/questions/...on-passed-to-another-function/7523699#7523699
If anyone can provide a version of gen_array that doesn't require
passing values to generators, and doesn't require globals, I'd be
elated to see it.
Cheers,
Andrew Pennebaker