2016-06-14 28 views
0

matlabコーダーから生成されたC++コードがありますが、配列のサイズを正しく設定する方法がわかりません。この配列のサイズを設定する方法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; 
} 

Iは、10x5のマトリクスを必要は10 [0] IV0を変更するために、それが正しい、argInit_real_T関数からデータを埋め? 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; 
} 
+0

を初期化するだろう変数を定義することが可能ですC++の関数のどこにでも置くことができます。例えば、forループが始まるところでは、for(int idx0 = 0; idx0 < result->サイズ[0UL]; idx0 ++) 'です。変数をできるだけローカルに保つことは、しばしば良い方法と考えられます。これはバグの数を減らす傾向があります。 – patrik

答えて

3

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行列のために、あなたは​​

+0

ありがとう、それは多くの助けとなりました。 – hanss

関連する問題