ma******@gmail.com wrote:
I'm looking for an equivalent approach to what I would do in C++ such
to create a vector of vector of complex. I would do :
Assuming C89 with no complex types:
struct complex {
double r,i;
};
struct vector {
void *data;
size_t size;
};
struct vector a;
a.data = malloc(NUM_VEC);
if (a.data == NULL)
handle_error();
a.size = NUM_VEC;
a.data[0] = create_complex_vector();
.....
push_value ( 3.4, -1, a.data[0]);
You'll obviously want to have the ability
to reallocate space as the vector grows, and
you'll want a more generic "constructor"
than create_complex_vector, but the
above demonstrates some ideas. You
wouldn't want to call malloc directly on
a.data, for example. Instead, you'll
want to write a function that takes perhaps
an initial size and an allocator function ptr
as parameters which will create the vector
for you. You might want to make the vector
an opaque type and hide the size_t variable
from the user....lots of ways to do this.