What data type needed to receive value from this function?-Collection of common programming errors
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 says NV_CONTENT_P(v)
is defined as:
#define NV_CONTENT_P(v) ( (N_VectorContent_Parallel)(v->content) )
So that version of the name is probably correct. I can’t see a definition for N_VectorContent_Parallel
on that page, but it’s probably defined somewhere as something like struct _N_VectorContent_Parallel*
. So, you can probably do:
N_VectorContent_Parallel v_cont1 = NV_CONTENT_P(u);
Remember that for structs, struct
is part of the type name. This means that you’re getting errors in your example because you haven’t included struct
:
// this is an unknown type
_N_VectorParallelContent *v_cont1;
// this is a "struct _N_VectorParallelContent"
struct _N_VectorParallelContent *v_cont1;
// But use this one, as it follows the macro
N_VectorContent_Parallel v_cont1;
If you want to see exactly what the preprocessor has done to your code, you can use gcc’s -E
flag.
-E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output. Input files which don't require preprocessing are ignored.
This is especially useful for seeing the results of macros and multiple complex header files.
Edit: From the source you’ve linked:
typedef struct _N_VectorContent_Parallel *N_VectorContent_Parallel;
This is a type definition that says that N_VectorContent_Parallel
is the same as a struct _N_VectorContent_Parallel *
(a pointer to a struct _N_VectorContent_Parallel
), which means you can access v_cont1
using the ->
syntax:
N_VectorContent_Parallel v_cont1;
printf("%d",v_cont1->local_length);
a->b
is 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.b
or a->b
.
Originally posted 2013-11-09 19:43:39.