评论:3D阵列 [英] Review: 3D-Array
问题描述
Hi clc,
这是分配3维
数组的正确方法吗?
/ *假设所有malloc'成功* /
#define MAT 2
#define ROW 2
#define COL 2
char *** ptr = malloc(MAT * sizeof * ptr);
for(i = 0; i< MAT; i ++)
ptr [i] = malloc(ROW * sizeof ** ptr);
for(i = 0; i< MAT; i ++)
for(j = 0; j< ROW; j ++)
ptr [i] [j] = malloc(COL * sizeof *** ptr);
谢谢。
-
当你愿意用餐时,霍姆斯先生?哈德森太太,好奇地问了房东太太。
七点半,后天。他说他参与了他的工作。
Hi c.l.c,
Is this a correct method of allocating a 3-dimensional
array?
/* assume all malloc''s succeed */
#define MAT 2
#define ROW 2
#define COL 2
char ***ptr = malloc ( MAT * sizeof *ptr );
for ( i = 0; i < MAT; i++ )
ptr[i] = malloc ( ROW * sizeof **ptr );
for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof ***ptr );
Thanks.
--
"When you will be pleased to dine, Mr. Homles?" asked Mrs Hudson,
the landlady, curiosly.
"At seven thirty, the day after tomorrow." said he invovled in his work.
推荐答案
" Vijay Kumar R Zanvar" < 6 ***** @ hotpop.com>在留言中写道
新闻:bv ************ @ ID-203837.news.uni-berlin.de ...
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> wrote in message
news:bv************@ID-203837.news.uni-berlin.de...
你好clc,
这是一个分配三维数组的正确方法吗?
/ *假设所有malloc'成功* /
#define MAT 2
#define ROW 2
#define COL 2
char *** ptr = malloc(MAT * sizeof * ptr);
> for(i = 0; i< MAT; i ++)
ptr [i] = malloc(ROW * sizeof ** ptr);
for(i = 0; i< MAT; i ++)
for(j = 0; j< ROW; j ++)
ptr [i] [j] = malloc(COL * sizeof *** ptr);
谢谢。
Hi c.l.c,
Is this a correct method of allocating a 3-dimensional
array?
/* assume all malloc''s succeed */
#define MAT 2
#define ROW 2
#define COL 2
char ***ptr = malloc ( MAT * sizeof *ptr );
for ( i = 0; i < MAT; i++ )
ptr[i] = malloc ( ROW * sizeof **ptr );
for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof ***ptr );
Thanks.
您定义指向指向char的指针的指针。所以你的第一个
malloc()应该为几个''指针指向一个指向
char'的指针创建空间并制作ptr(这是指向一个指针的指针) ;指向char的指针"
通过malloc调用指向它
char *** ptr = malloc(MAT * sizeof ** ptr );
对于指向char的指针数组中的每个条目,你现在需要指向一个''指向char的指针''键入:
for(i = 0; i< MAT; i ++)
ptr [i] = malloc(ROW * sizeof * ptr);
所以现在你有一堆ROW指针指向数组的每一行中的字符
ptr [i]所以你现在必须将它们指向' 'char''(或
char数组中的第一个字符)
for(i = 0; i< MAT; i ++)
j $ 0; j
ptr [i] [j] = malloc(COL * sizeof char);
你可以更清楚地想到它:
char arra y [MAT] [ROW] [COL];
char * array [MAT] [ROW]
char ** array [MAT ]
char ***数组;
在实践中,sizeof *** ptr和sizeof ** ptr等都是一样的。
因此,您在原始代码中得到的结果是char *类型的三维
数组,如下所示:
char * array [MAT] [ROW] [COL];
这可能是你想要的,但你创建的所有char *数组条目
还没有指向任何地方,当你把它们指向某个地方时,你会在
这样的事实最终得到一个像这样的4维数组:
char array [MAT] [ROW] [COL] [STRLEN + 1];
希望这有助于你澄清这个......
Sean
You define a pointer to a pointer to a pointer to a char. So your first
malloc() should create the space for several ''pointer to a pointer to a
char'' and make ptr (which is a pointer to a "pointer to a pointer to char"
point to it through the malloc call)
char *** ptr=malloc(MAT * sizeof **ptr);
For each of the entries in this array of pointers to pointers to a char you
now need to point to a ''pointer to a char'' type:
for(i=0;i<MAT;i++)
ptr[i]=malloc(ROW * sizeof *ptr);
So now you have a bunch of ROW pointers to chars in each row of the array
ptr[i] so now you must point these to the ''char'' (or the first char in an
array of char)
for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof char );
You can think of it more clearly like this:
char array[MAT][ROW][COL];
char * array[MAT][ROW]
char ** array[MAT]
char *** array;
In practice the sizeof ***ptr and sizeof ** ptr etc will all be the same.
Therefore what you end up with in your original code is a three dimentional
array of the type char * as follows:
char * array[MAT][ROW][COL];
Which might be what you wanted, but all the char * array entries you created
do not yet point anywhere and when you do point them somewhere you will in
fact end up with a 4 dimensional array like this for example:
char array[MAT][ROW][COL][STRLEN+1];
Hope this helps you clarify this...
Sean
Sean Kenwrick写道:
Sean Kenwrick wrote:
" Vijay Kumar R Zanvar" < 6 ***** @ hotpop.com>在消息中写道
新闻:bv ************ @ ID-203837.news.uni-berlin.de ...
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> wrote in message
news:bv************@ID-203837.news.uni-berlin.de...
你好clc,
这是一个分配三维数组的正确方法吗?
/ *假设所有malloc'成功* /
#define MAT 2
#define ROW 2
#define COL 2
char *** ptr = malloc(MAT * sizeof * ptr);
> for(i = 0; i< MAT; i ++)
ptr [i] = malloc(ROW * sizeof ** ptr);
for(i = 0; i< MAT; i ++)
for(j = 0; j< ROW; j ++)
ptr [i] [j] = malloc(COL * sizeof *** ptr);
你定义一个指向指向char的指针的指针。所以你的第一个malloc()应该创建几个指向
char'的指针的空间,然后创建ptr(指向指向char的指针的指针)。
指向它通过malloc调用)
char *** ptr = malloc(MAT * sizeof ** ptr);
Hi c.l.c,
Is this a correct method of allocating a 3-dimensional
array?
/* assume all malloc''s succeed */
#define MAT 2
#define ROW 2
#define COL 2
char ***ptr = malloc ( MAT * sizeof *ptr );
for ( i = 0; i < MAT; i++ )
ptr[i] = malloc ( ROW * sizeof **ptr );
for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof ***ptr );
Thanks.
You define a pointer to a pointer to a pointer to a char. So your first
malloc() should create the space for several ''pointer to a pointer to a
char'' and make ptr (which is a pointer to a "pointer to a pointer to char"
point to it through the malloc call)
char *** ptr=malloc(MAT * sizeof **ptr);
你确定吗?是不是sizeof ** ptr相当于sizeof char *
看到ptr是char ***类型?也许你会混淆** ptr与
" char **"?
我认为你应该使用
char *** ptr = malloc( MAT * sizeof * ptr);
而不是这里。
对于这个指向char的指针数组中的每个条目你都要
现在需要指向一个''指向char的指针''类型:
for(i = 0; i< MAT; i ++)
ptr [i] = malloc(ROW * sizeof * PTR);
类似地* ptr与char *不同。使用:
ptr [i] = malloc(ROW * sizeof *(ptr [i]));
所以现在你有一堆指向每个字符的ROW指针阵列的一行
ptr [i]所以现在你必须将它们指向''char''(或者是一个char的
数组中的第一个char)
for( i = 0; i< MAT; i ++)
for(j = 0; j< ROW; j ++)
ptr [i] [j] = malloc(COL * sizeof char);
这里:
ptr [i] [j] = malloc(COL * sizeof *(ptr [i] [j]));
char数组[MAT] [行] [COL];
char *数组[MAT] [行]
char **数组[MAT]
char ***数组;
实践中sizeof ** * ptr和sizeof ** ptr等都是一样的。
因此你在原始代码中最终得到的是char *类型的三维数组,如下所示:
char * array [MAT] [ROW] [COL];
我会有更像char数组[MAT] [行] [COL];
数组有三套括号所以它衰变为三指针
对吗?这正是OP的开始。
你可能想要的是什么,但是你创建的所有char *数组条目还没有指向任何地方,当你做的时候将它们指向你将会在某个地方最终得到一个像这样的4维数组:
char数组[MAT] [行] [COL] [STRLEN + 1];
肖恩
Are you sure about that? Isn''t sizeof **ptr equivalent to sizeof char*
seeing as ptr is of type char***? Perhaps you are confusing "**ptr" with
"char**"?
I think you should use
char*** ptr = malloc(MAT * sizeof *ptr);
here instead.
For each of the entries in this array of pointers to pointers to a char you
now need to point to a ''pointer to a char'' type:
for(i=0;i<MAT;i++)
ptr[i]=malloc(ROW * sizeof *ptr);
Similarly "*ptr" is not the same as "char*". Use:
ptr[i] = malloc(ROW * sizeof *(ptr[i]));
So now you have a bunch of ROW pointers to chars in each row of the array
ptr[i] so now you must point these to the ''char'' (or the first char in an
array of char)
for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof char );
And here:
ptr[i][j] = malloc(COL * sizeof *(ptr[i][j]));
You can think of it more clearly like this:
char array[MAT][ROW][COL];
char * array[MAT][ROW]
char ** array[MAT]
char *** array;
In practice the sizeof ***ptr and sizeof ** ptr etc will all be the same.
Therefore what you end up with in your original code is a three dimentional
array of the type char * as follows:
char * array[MAT][ROW][COL];
I''d have though it was more like char array[MAT][ROW][COL];
The array has three sets of braces so it decays to a triple pointer
right? Which is exactly what the OP started with.
Which might be what you wanted, but all the char * array entries you created
do not yet point anywhere and when you do point them somewhere you will in
fact end up with a 4 dimensional array like this for example:
char array[MAT][ROW][COL][STRLEN+1];
Hope this helps you clarify this...
Sean
嗯 - 我对所有人都感到困惑这个...
Hmmm - I''m a bit confused by all this...
Vijay Kumar R Zanvar写道:
Vijay Kumar R Zanvar wrote:
你好clc,
/ *假设所有malloc'成功* /
#define MAT 2
#define ROW 2
#define COL 2
char *** ptr = malloc(MAT * sizeof * ptr);
for(i = 0; i < MAT; i ++)
对于(i = 0; i& LT; MAT; i ++)
for(j = 0; j< ROW; j ++)
ptr [i] [j] = malloc(COL * sizeof *** ptr);
谢谢。
Hi c.l.c,
Is this a correct method of allocating a 3-dimensional
array?
/* assume all malloc''s succeed */
#define MAT 2
#define ROW 2
#define COL 2
char ***ptr = malloc ( MAT * sizeof *ptr );
for ( i = 0; i < MAT; i++ )
ptr[i] = malloc ( ROW * sizeof **ptr );
for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof ***ptr );
Thanks.
正确。请注意,作为之前在此发布过多次的人,您需要知道这些内容已在常见问题解答中得到解答。特别是Q6.16:
http: //www.eskimo.com/~scs/C-faq/q6.16.html
-nrk。
-
删除电子邮件的devnull
Correct. Note that as someone who''s posted here more than once before, you
must know this is answered in the FAQ. Specifically Q6.16:
http://www.eskimo.com/~scs/C-faq/q6.16.html
-nrk.
--
Remove devnull for email
这篇关于评论:3D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!