2008-08-06 25 views

答えて

19

これは任意のデータ型で実行できます。単にそれへのポインタのポインタます

typedef struct { 
    int myint; 
    char* mystring; 
} data; 

data** array; 

しかし、あなたはまだ変数ををmallocする必要があり、それは少し複雑を取得していることを忘れないでください:

//initialize 
int x,y,w,h; 
w = 10; //width of array 
h = 20; //height of array 

//malloc the 'y' dimension 
array = malloc(sizeof(data*) * h); 

//iterate over 'y' dimension 
for(y=0;y<h;y++){ 
    //malloc the 'x' dimension 
    array[y] = malloc(sizeof(data) * w); 

    //iterate over the 'x' dimension 
    for(x=0;x<w;x++){ 
    //malloc the string in the data structure 
    array[y][x].mystring = malloc(50); //50 chars 

    //initialize 
    array[y][x].myint = 6; 
    strcpy(array[y][x].mystring, "w00t"); 
    } 
} 

割り当てを解除するためのコードを構造は似ています - あなたが割り当てたすべてにfree()を呼び出すことを忘れないでください! (堅牢なアプリケーションでは、check the return of malloc()

これを関数に渡したいとしましょう。あなたはおそらく、データ構造ではなく、データ構造のポインタへのポインタで操作をしたいので、あなたはまだ、二重のポインタを使用することができます。

int whatsMyInt(data** arrayPtr, int x, int y){ 
    return arrayPtr[y][x].myint; 
} 

でこの関数を呼び出した:

printf("My int is %d.\n", whatsMyInt(array, 2, 4)); 

出力:

My int is 6. 
+0

ヘルプが必要です:http://stackoverflow.com/questions/16943909/manipulate-multidimensional-array-in-a-function – Dchris

+3

ポインタセグメントルックアップテーブルへのポインタは2D配列ではありません。 '[] []'構文が許されているからといって、魔法のように配列に変わることはありません。メモリは配列に必要な隣接するメモリセルに割り当てられていないので、memcpy()などはできません。ルックアップテーブルはむしろヒープ全体に散らばり、ルックアップが遅くなり、ヒープが断片化します。 – Lundin

31

は、別個のパラメータとして配列の次元を持つ最初の要素を明示的にポインタを渡します。

... 
int arr1[10][20]; 
int arr2[5][80]; 
... 
func_2d(&arr1[0][0], 10, 20); 
func_2d(&arr2[0][0], 5, 80); 

同じ原理は、より高い次元の配列に適用されると呼び出されます

void func_2d(int *p, size_t M, size_t N) 
{ 
    size_t i, j; 
    ... 
    p[i*N+j] = ...; 
} 

func_3d(int *p, size_t X, size_t Y, size_t Z) 
{ 
    size_t i, j, k; 
    ... 
    p[i*Y*Z+j*Z+k] = ...; 
    ... 
} 
... 
arr2[10][20][30]; 
... 
func_3d(&arr[0][0][0], 10, 20, 30); 
+2

'p [i * Y + j * Z + k]'は 'p [i * Y * Z + j * Z + k]'でなければなりません。 –

+0

http://stackoverflow.com/questions/16943909/manipulate-multidimensional-array-in-a-function – Dchris

+0

iとjの値はどうなりますか? –

-2
int matmax(int **p, int dim) // p- matrix , dim- dimension of the matrix 
{ 
    return p[0][0]; 
} 

int main() 
{ 
    int *u[5]; // will be a 5x5 matrix 

    for(int i = 0; i < 5; i++) 
     u[i] = new int[5]; 

    u[0][0] = 1; // initialize u[0][0] - not mandatory 

    // put data in u[][] 

    printf("%d", matmax(u, 0)); //call to function 
    getche(); // just to see the result 
} 
例えば、任意のintの2次元配列のサイズのを処理します
+0

http://stackoverflow.com/questions/16943909/manipulate-multidimensional-array-in-a-function – Dchris

+1

これは2次元配列ではなく、ルックアップテーブルです。また、これにはタグが付けられています。 – Lundin

15

あなたの関数を次のように宣言できます。

f(int size, int data[][size]) {...} 

コンパイラはすべてのポインタ演算を行います。

の前に寸法のサイズがでなければならないことに注意してください。

GNU Cは、(場合にあなたが本当に配列後の寸法を渡す必要がある)引数の宣言を転送することができます:

f(int size; int data[][size], int size) {...} 

最初の次元、あなたがあまりにも引数として渡すことができますが、Cコンパイラのために役に立ちません(sizeof演算子であっても、引数として渡された配列の上に適用すると、常に扱われます)は、最初の要素へのポインタです。

+1

IMOこれは受け入れられる回答でなければなりません。余分なコードは不要で、不要なヒープ割り当ては不要です。シンプルでクリーンな – imkendal

+0

ありがとう@kjh、私はこれもクリーンなソリューションだと思う。受け入れられた答えは、彼のために働いた答えです。見て:OPは私の答えのほぼ6年前の2008年からです。それ以外にも、私がここで使った構文がC言語の標準に許されているかどうかはわかりません。 – rslemos

+0

これは、関数の引数としてサイズM x Nの整数行列(2次元配列)を渡すために私が最終的に採用したソリューションです。関数のプロトタイプは次のようなものです:void f(int N、int data [] [N]、int M);関数の本体では、element [m] [n]はdata [m] [n]として書くことができます。非常に便利です。インデックス計算は不要です。 – jonathanzh

関連する問題