I need help figuring out the correct data type for an assignment from a function call please.
I’m trying to get at the data in the content field of N_Vector u. Here’s what the documentation says about N_Vector:
The type
N_Vectoris defined asN_Vector u; tpedef struct _generic_N_Vector *N_Vector; struct _generic_N_Vector { void *content; struct _generic_N_Vector_Ops *ops; };…
[The parallel NVECTOR module] defines the content field of
N_Vector
to be a structure containing global and local lengths, a pointer to the
beginning of contiguous local data array, MPI communicator and flag.struct _N_VectorContent_Parallel { long int local_length; long int global_length; booleantype own_data; realtype *data; MPI_Comm comm; }
So I guess that means that content in _generic_N_Vector “points to” a structure of type _N_VectorContent_Parallel (right?).
Then I try to use a macro for accessing content. Here’s the documentation for NV_CONTENT_P.
v_cont=NV_CONTENT_P(v)setsv_contto be a pointer to theN_Vectorcontent
structure of typestruct _N_VectorParallelContent.
Notice the different name of the struct!
What does that mean? What type do I declare v_cont to be?
I tried
N_Vector u;
...
_N_VectorParallelContent *v_cont1;
_N_VectorContent_Parallel *v_cont2;
v_cont1 = NV_CONTENT_P(u);
v_cont2 = NV_CONTENT_P(u);
but these declarations got the error “‘_N_VectorContent_Parallel’ undeclared…” or “‘_N_VectorParallelContent’ undeclared…”.
But it seems that these structures must be delcared already. I successfully declared (and used) u, of type N_Vector. And the docs seem to say that N_Vector contains one of those two structures (or maybe both).
So why the error message? What is the correct data type to declare for v_cont to receive data from NV_CONTENT_P?
I know this is a long, detailed question, but I don’t understand enough to whittle it down any more.
Thanks for your help.
I’m not familiar with this particular library, but it looks to me like the documentation is a little inconsistent.
Right after the blurb about
NV_CONTENT_P(v), it saysNV_CONTENT_P(v)is defined as:So that version of the name is probably correct. I can’t see a definition for
N_VectorContent_Parallelon that page, but it’s probably defined somewhere as something likestruct _N_VectorContent_Parallel*. So, you can probably do:Remember that for structs,
structis part of the type name. This means that you’re getting errors in your example because you haven’t includedstruct:If you want to see exactly what the preprocessor has done to your code, you can use gcc’s
-Eflag.This is especially useful for seeing the results of macros and multiple complex header files.
Edit: From the source you’ve linked:
This is a type definition that says that
N_VectorContent_Parallelis the same as astruct _N_VectorContent_Parallel *(a pointer to astruct _N_VectorContent_Parallel), which means you can accessv_cont1using the->syntax:a->bis is shorthand for(*a).b– it’s just a cleaner-looking way of writing the dereference needed to accessing a member of a struct through a pointer to that struct. If that seems confusing, see my answer to this question.Personally, I don’t like typedefs that hide pointers like this one, because it’s hard to tell by looking at the code whether you need to use
a.bora->b.