正在将指针地址从二维数组传输到结构
Transferring pointer addresses from 2-dimensional array to structure
这是我在这里的第一个输入,所以我很抱歉有任何方面不符合规则。
我必须用C++接口连接两个应用程序。应用程序A将要交换的值存储在二维动态数组双**参数中。应用程序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中编写的out和in参数调用另一个函数。但首先我想通过一个小计算连接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"的实体。因此,指针的交换一定有问题。到目前为止,我有两个问题:
- 如何实现将二维动态数组Parameters的地址存储在结构指针中
- 如何实现指针输出存储结果新计算值的地址
感谢您并致以最良好的问候
根据结构体Inputs
:的定义
typedef struct {
double Input1;
int Input2;
} Inputs;
并给出了Parameters
作为的定义
double** Parameters;
说
in.Input1[0] = *Parameters[0];
格式不正确,因为Input1
是double
,无法对其进行索引。你想做的是在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的地址存储在另一个doubles中,然后再去引用那个地址?这不是一件安全的事情,但如果你绝对必须执行这样的工作,以下是如何使用union
技巧将double*
双关为double
:
首先声明一个可以容纳double*
和double
:的并集
// Warning, this is not recommended
union doubleToPtr{
double* ptr;
double dbl;
};
现在我们可以放入一个指针,然后取出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 = ¶meter;
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
,并看到我们已经成功地进行了:
std::cout << toAssignTo << std::endl;
4
实时演示
非常感谢您快速而详细的回答。弄清楚我想去哪里让我很沮丧。
我找到了一种不那么危险的双关语的方法。结构在另一个.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;
你能说出有什么不同吗?
感谢
- 将值从二维数组输出到文本文件
- 在二维数组中查找最小值和最大值?
- 移动二维数组中的字符
- 如何正确填充在堆上分配的二维数组?
- 传递二维数组时出现问题
- 具有随机数的二维数组不会更改
- 如何在C++中获取二维数组中最少的一列数?
- 如何使用用户输入变量制作二维数组?
- C++ 中动态二维数组的访问冲突
- 遍历二维数组的所有子数组
- 将二维数组的所有元素插入到一维数组中
- 在函数中传递二维数组
- 为什么指针在对二维数组进行排序时无法正常工作?
- 创建 std::string 的二维数组的最佳做法
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 元素在二维数组 c++ 中的出现次数
- 执行 c++ 二维数组
- C++ 中的二维数组初始化为一个值
- 在C++中初始化第一维大小未知的二维数组
- 正在将指针地址从二维数组传输到结构