问题描述
我看到了有线代码的主题。关于指向阵列并知道一点
。但是我仍然对以下问题感到困惑:
我定义了一个指向数组的点'b"
int(* b)[100];
然后我将内存定位到b,使用malloc
b =(???)malloc(10 * sizeof(* b))
这里,如果我想用力转换类型,我应该使用哪个?
我使用:
b =(int **)malloc(10 * sizeof(* b)) ;
代码可以编译,但是有一个warring"赋值来自
不兼容的指针类型"
thx
不要从malloc投出返回值。
b = malloc(10 * sizeof(* b);
如果你' '问一下b的类型是'int(*)[100];
这就是你得到的结果你不应该这样做。
不要这样做。
不要从malloc投出返回值。
b = malloc(10 * sizeof(* b);
如果你' '问一下b的类型是'int(*)[100];
这就是你得到的结果你不应该这样做。
不要这样做。
感谢您的回复如此之快
正确的演员阵容是(int(*)[100])。
但是你应该_not_强制回报malloc的价值。
-
Roland Csaszar ----------- \\\ /// - ------------ +43 316 495 2129
软件开发------ \\\ /// -------- ---
KNAPP物流自动化 - \\\\ // mailto:ro ************ @ knapp.com
I saw the topic of "wired code " about "point to array" and know a
little about it. But I am still confused about the question below:
I define a point to array "b"
int (*b)[100];
then I locate the memory to b,using "malloc"
b= (???) malloc( 10*sizeof ( *b ) )
Here,if I want to convert the type by force, which shall I use?
I use:
b=(int**)malloc(10*sizeof(*b));
the code can compile, but there is a warring "assignment from
incompatible pointer type"
thx
Don''t cast the return value from malloc.
b= malloc( 10*sizeof ( *b ) ;
If you''re asking what the type of b is it''s an int(*)[100];
That''s what you get when you cast to something you should not.
Don''t do that.
Don''t cast the return value from malloc.
b= malloc( 10*sizeof ( *b ) ;
If you''re asking what the type of b is it''s an int(*)[100];
That''s what you get when you cast to something you should not.
Don''t do that.
Thanks for your reply so quickly
The correct cast would be (int (*)[100]).
But you should _not_ cast the return value of malloc.
--
Roland Csaszar ----------- \\\ /// -------------- +43 316 495 2129
Software Development ------ \\\ /// ----------- http://www.knapp.com
KNAPP Logistics Automation - \\V// - mailto:ro************@knapp.com
这篇关于“指向阵列”的类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!