【问题标题】:What data type needed to receive value from this function?从这个函数接收值需要什么数据类型?
【发布时间】:2012-09-03 20:30:54
【问题描述】:

我需要帮助,从函数调用中找出正确的数据类型。

我正在尝试获取N_Vector ucontent 字段中的数据。以下是文档中关于 N_Vector 的内容:

N_Vector 类型定义为

N_Vector u;
tpedef struct _generic_N_Vector *N_Vector;
struct _generic_N_Vector {
  void *content;
  struct _generic_N_Vector_Ops *ops;
};

...

[并行NVECTOR模块]定义N_Vectorcontent字段 是一个包含全局和局部长度的结构,指向 连续本地数据数组、MPI 通信器和标志的开头。

struct _N_VectorContent_Parallel {
  long int local_length;
  long int global_length;
  booleantype own_data;
  realtype *data;
  MPI_Comm comm;
}

所以我猜这意味着_generic_N_Vector 中的content“指向”_N_VectorContent_Parallel 类型的结构(对吗?)。

然后我尝试使用宏来访问content。这是NV_CONTENT_P 的文档。

v_cont=NV_CONTENT_P(v)v_cont 设置为指向N_Vector 内容的指针 struct _N_VectorParallelContent 类型的结构。

注意结构的不同名称!

这是什么意思?我声明v_cont 是什么类型?

我试过了

N_Vector u;
...
_N_VectorParallelContent *v_cont1;
_N_VectorContent_Parallel *v_cont2;
v_cont1 = NV_CONTENT_P(u);
v_cont2 = NV_CONTENT_P(u);

但是这些声明得到了错误“'_N_VectorContent_Parallel' undeclared...”或“'_N_VectorParallelContent' undeclared...”。

但似乎这些结构必须已经被删除了。我成功声明(并使用)u,类型为N_Vector。并且文档似乎说N_Vector 包含这两种结构之一(或两者兼有)。

那么为什么会出现错误消息?为 v_cont NV_CONTENT_P 接收数据声明的正确数据类型是什么?

我知道这是一个冗长而详细的问题,但我理解的不够深入,无法再精简。 感谢您的帮助。

【问题讨论】:

  • @Timothy 间接。实际上使用 mpicc,它是(我被告知)gcc 的包装器。

标签: c types


【解决方案1】:

我不熟悉这个特定的库,但在我看来 the documentation 有点不一致。

在关于NV_CONTENT_P(v) 的简介之后,它说NV_CONTENT_P(v) 被定义为:

#define NV_CONTENT_P(v) ( (N_VectorContent_Parallel)(v->content) )

所以那个版本的名字可能是正确的。我在该页面上看不到N_VectorContent_Parallel 的定义,但它可能在某处定义为struct _N_VectorContent_Parallel*。所以,你可能可以这样做:

N_VectorContent_Parallel v_cont1 = NV_CONTENT_P(u);

请记住,对于结构,struct 是类型名称的一部分。这意味着您的示例中出现错误,因为您没有包含 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;

如果你想确切地看到预处理器对你的代码做了什么,你可以使用 gcc 的-E 标志。

-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.

这对于查看宏和多个复杂头文件的结果特别有用。


编辑:来自您链接的来源:

typedef struct _N_VectorContent_Parallel *N_VectorContent_Parallel;

这是一个类型定义,表示N_VectorContent_Parallelstruct _N_VectorContent_Parallel * 相同(指向struct _N_VectorContent_Parallel 的指针),这意味着您可以使用-> 语法访问v_cont1

N_VectorContent_Parallel v_cont1;
printf("%d",v_cont1->local_length);

a->b(*a).b 的简写——它只是一种更简洁的方式来编写通过指向该结构的指针访问结构成员所需的取消引用。如果这看起来令人困惑,请参阅我对 this question 的回答。

就我个人而言,我不喜欢像这样隐藏指针的 typedef,因为通过查看代码很难判断您需要使用 a.b 还是 a->b

【讨论】:

  • 谢谢,蒂姆。它编译并执行。现在我想弄清楚如何查看v_cont 包含的内容(我删除了1),看看我是否能得到我想要的。这篇文章 (-E) 中也有很好的提示。
  • FWIW:我可能找到了N_VectorContent_Parallel 的来源。这里:simtk.org/api_docs/simtkcore/api_docs20/…
  • Jeff:我在最后添加了一点关于如何访问结构成员的内容。不确定您是否需要它,但它可能会有所帮助:)
  • 谢谢,@Timothy。这很有用,因为我经常对 C 的指针语法感到困惑。但现在我只是查看gdb 中的数据,以了解它返回的是全局向量还是局部向量。不幸的是,您发布的所有工作看起来 NV_CONTENT_P 仍然只返回本地向量。 :( 似乎没有返回全局向量的方法,所以这就是我仍在寻找的(但至少我在此过程中学到了一些东西)。
猜你喜欢
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 2010-09-07
  • 1970-01-01
  • 2021-01-28
相关资源
最近更新 更多