array of array of complex things

M

ma740988

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 :

typedef std::vector < std::complex < double > > complex_dvec;
typedef std::vector < complex_dvec > twod_complex_vec;

int main() {
twod_complex_vec tdv ( 3, 3 );
for ( size_t idx ( 0 ); idx < tdv.size(); ++idx )
{
for ( size_t jdx ( 0 ); jdx < tdv.front().size(); ++jdx )
tdv [ idx ] [ jdx ] = idx * jdx ;
}
}

I'm trying to come up with an equivalent C approach to this but I'm
going around in circles. Source snippet appreaciated.. Thanks in
advance for
 
D

dbtid

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 :

typedef std::vector < std::complex < double > > complex_dvec;
typedef std::vector < complex_dvec > twod_complex_vec;

int main() {
twod_complex_vec tdv ( 3, 3 );
for ( size_t idx ( 0 ); idx < tdv.size(); ++idx )
{
for ( size_t jdx ( 0 ); jdx < tdv.front().size(); ++jdx )
tdv [ idx ] [ jdx ] = idx * jdx ;
}
}

I'm trying to come up with an equivalent C approach to this but I'm
going around in circles. Source snippet appreaciated.. Thanks in
advance for

http://www.c-faq.com/aryptr/dynmuldimary.html
 
B

Bill Pursell

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top