質問
Cでしていただくために、多次元配列としての機能を単一の引数がどうなるかはわからない寸法にな配列になるだろうか?
また私の多次元配列を含む場合があり種類以外の文字列です。
解決
ってこんな風にしてできるんだデータ型でもかまいません。だけでポインタをポインター:
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");
}
}
のコードを修復させようとするとエラーがその構造は似てな通話無料)がすべmalloced!(また、堅調に推移し、アプリケーションは チェックの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.
他のヒント
パス明示的にポインタの最初の要素の配列寸法として別々のパラメータ。例えば、取扱うための任意のサイズの2次元配列のint:
void func_2d(int *p, size_t M, size_t N)
{
size_t i, j;
...
p[i*N+j] = ...;
}
と言われるくらいとして
...
int arr1[10][20];
int arr2[5][80];
...
func_2d(&arr1[0][0], 10, 20);
func_2d(&arr2[0][0], 5, 80);
同様の原則が適用される高次元配列:
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);
で確定申告としての機能:
f(int size, int data[][size]) {...}
のコンパイラはその全てのポインタを演算します。
なお、寸法サイズ表示が付されることを条件と 前 の配列そのものです。
GNU Cを可能に引数の宣言に転送できない場合には本当に渡す必要があり寸法後の配列):
f(int size; int data[][size], int size) {...}
の寸法ができるパスを引数として与えた場合、そのものは無理のためのCコンパイラ(もsizeofオペレーターが応用上の配列として渡された引数は常に処理がポインタを最初の要素).
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
}
所属していません StackOverflow