我得到了从 matlab 编码器生成的 C++ 代码,但我不确定如何正确设置数组的大小。
static emxArray_real_T *argInit_d7351x5_real_T()
{
emxArray_real_T *result;
static int iv0[2] = { 2, 5 };
int idx0;
int idx1;
// Set the size of the array.
// Change this size to the value that the application requires.
result = emxCreateND_real_T(2, *(int (*)[2])&iv0[0]);
// Loop over the array to initialize each element.
for (idx0 = 0; idx0 < result->size[0UL]; idx0++) {
for (idx1 = 0; idx1 < 5; idx1++) {
// Set the value of the array element.
// Change this value to the value that the application requires.
result->data[idx0 + result->size[0] * idx1] = argInit_real_T();
}
}
return result;
}
//
// Arguments : void
// Return Type : double
//
static double argInit_real_T()
{
return 1.0;
}
我需要一个 10x5 矩阵填充来自 argInit_real_T 函数的数据,将 iv0[0] 更改为 10 是否正确? int (*)[2] 命令如何工作?
struct emxArray_real_T
{
double *data;
int *size;
int allocatedSize;
int numDimensions;
boolean_T canFreeData;
};
emxArray_real_T *emxCreateND_real_T(int numDimensions, int *size)
{
emxArray_real_T *emx;
int numEl;
int i;
emxInit_real_T(&emx, numDimensions);
numEl = 1;
for (i = 0; i < numDimensions; i++) {
numEl *= size[i];
emx->size[i] = size[i];
}
emx->data = (double *)calloc((unsigned int)numEl, sizeof(double));
emx->numDimensions = numDimensions;
emx->allocatedSize = numEl;
return emx;
}
最佳答案
int(*)[2]
不是命令 - 它声明了一个指向长度为 2 的 int 数组的指针。
现在让我们看一下:*(int (*)[2])&iv0[0]
。首先取iv0的第一个元素的地址,类型为int*
,将其转换为指向int[2]
的指针(即int( *)[2]
),然后再次取消引用,取回 int[2]
。当传递给 emxCreateND_real_T
时,它会再次提升为 int*
。
实际上,如果您直接传递 iv0 也会发生同样的情况...
result = emxCreateND_real_T(2, iv0);
是的,对于 10x5 矩阵,您将初始化 static int iv0[] = { 10, 5 };
关于c++ - 如何设置此数组的大小 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37820694/