将指针地址从二维数组传输到结构

时间:2015-11-24 13:22:47

标签: c++ arrays pointers struct

这是我在这里的第一个输入,所以我很抱歉任何不符合规则的方面。

我必须通过C ++接口连接两个应用程序。应用程序A将要交换的值存储在二维动态数组double **参数中。应用程序A调用函数:

void __cdecl function(double** Parameters)

应用程序B应将应用程序A的值存储在如下定义的结构中:

typedef struct {
  double Input1;
  int Input2;
} Inputs;           // struct for input variables

typedef struct {
  double Output1;
  int Output2;
} Outputs;          //struct for output variables

目的是调用带有参数的另一个函数,并在应用程序B中编写。但首先,我想通过一个小的计算连接Output1和Input1。所以我定义:

Double result;
Inputs in;      // name of a struct of type inputs
Outputs *out;   //name of the pointer for a struct of the type outputs

in.Input1[0] = *Parameters[0];      //value from Parameters[0] is put on in.Input[0]
out->Output1 = Parameters[1];       //pointer out shall store the address that is stored in Parameters[1]
result = 2 / in.Input1[0];      //calculate result
*(out)->Output1 = result;       //store the address of the value of result in the pointer out

我收到一条错误消息。 “类型”“double *”“的值不能分配给”“double”“的实体。所以交换指针一定有问题。到目前为止我有两个问题:

  1. 如何实现二维动态数组参数的地址存储在结构指针中?
  2. 如何实现指针输出存储新计算的结果值的地址?
  3. 谢谢你,最诚挚的问候

2 个答案:

答案 0 :(得分:1)

按照结构Inputs的定义:

typedef struct {
  double Input1;
  int Input2;
} Inputs;  

并将Parameters定义为

double** Parameters;

in.Input1[0] = *Parameters[0];

格式不正确,因为Input1double,您无法将其编入索引。你要做的是在Input1存储一个不安全的地址,但是如果你想尝试它,你可以尝试打字(参见我的编辑)。

存在类似问题
out->Output1 = Parameters[1]; // Output1 is a double, Parameters[1] is a double*

result = 2 / in.Input1[0]; //Input1 is a double, cannot index into it

修改

所以你想将double的地址存储在另一个double中,然后再取消引用该地址?这不是一件安全的事情,但是如果您绝对必须执行这样的解决方法,那么您可以使用{{1}来double*作为double。诀窍:

首先声明一个可以容纳uniondouble*的联盟:

double

现在我们可以将指针放入,然后取出// Warning, this is not recommended union doubleToPtr{ double* ptr; double dbl; }; ,反之亦然。

首先让我们将指针存储为double:

double

接下来,在另一方面,我们可以使用另一个联合来接收// seriously, don't do this unless you're forced to double parameter = 4.0; double holdingAddress; double toAssignTo; // first pun the address of a double into a double // by assigning to ptr in union, then reading out as double doubleToPtr typePunnerIn; typePunnerIn.ptr = &parameter; holdingAddress = typePunnerIn.dbl; (我们知道它确实是一个地址),然后将其解释为double

double*

然后我们可以打印// third warning, ask yourself if you really need this // next pun the double into a double address // by assigning double into union // then read out as address doubleToPtr typePunnerOut; typePunnerOut.dbl = holdingAddress; toAssignTo = *(typePunnerOut.ptr); 并看到我们已成功进行:

toAssignTo
  

4

Live Demo

答案 1 :(得分:0)

非常感谢您快速而详细的回答。为了弄清楚我想去哪里,它让我很感兴趣。

我发现了一种危险类型的方式。结构在另一个.h文件中定义:

 typedef struct {
  double Input1;
  int Input2;
} Inputs;           // struct for input variables

typedef struct {
  double Output1;
  int Output2;
} Outputs;          //struct for output variables

主要功能现在看起来像这样:

void __cdecl function(double** Parameters)
{   Inputs in1, *in;        //Define variable and pointer of Inputs 
    Outputs out1, *out;     //Define variable and pointer of Outputs
    double result;

    in = &in1;     //get the adress of the struct in1
    out = &out1;   //get the adress of the struct out1

    in1.Input1 = *dParameters[0];   //get the first value from the Array dParameters
    result = 2 / in.Input1          //calculate result

    *(dParameters[1]) = out1.Output1;   //rewrite value in the address on which the pointer dParameters[1] is pointing
}

我希望我能正确解释。另一个问题:我找到了两种在结构上定义指针的方法

struct Inputs *ptr;   //or
Inputs *ptr;

你能说出有什么区别吗?

谢谢