来自不兼容指针类型警告的初始化。找不到问题
我假设此警告会使我的应用程序崩溃。我正在将 Objective-C 用于 iOS 应用程序。 Xcode 不提供堆栈跟踪或任何内容。没有帮助。
我将此赋值作为全局变量:
int search_positions[4][6][2] = {{{0,-2},{0,1},{1,-1},{-1,-1},{1,0},{-1,0}}, //UP
{{-2,0},{1,0},{-1,1},{-1,-1},{0,1},{0,-1}}, //LEFT
{{0,2},{0,-1},{1,1},{-1,1},{1,0},{-1,0}}, //DOWN
{{2,0},{-1,0},{1,1},{1,-1},{0,1},{0,-1}} //RIGHT
};
因此 search_positions 不是指向整数指针的指针吗?
为什么这给出“从不兼容的指针初始化”?
int ** these_search_positions = search_positions[current_orientation];
当然,这只是从数组中获取一个指向整数指针的指针,并由 current_orientation 偏移?
我在这里缺少什么?我以为我现在已经知道指针了。 :(
谢谢。
I'm assuming this warning is crashing my app. I'm using objective-c for an iOS app. Xcode doesn't give a stack trace or anything. Not helpful.
I have this assignment as a global variable:
int search_positions[4][6][2] = {{{0,-2},{0,1},{1,-1},{-1,-1},{1,0},{-1,0}}, //UP
{{-2,0},{1,0},{-1,1},{-1,-1},{0,1},{0,-1}}, //LEFT
{{0,2},{0,-1},{1,1},{-1,1},{1,0},{-1,0}}, //DOWN
{{2,0},{-1,0},{1,1},{1,-1},{0,1},{0,-1}} //RIGHT
};
Wouldn't search_positions therefore be a pointer to a pointer to an integer pointer?
Why does this give "Initialisation from incompatible pointer"?
int ** these_search_positions = search_positions[current_orientation];
Surely this just takes a pointer to an integer pointer from the array, offseted by current_orientation?
What I am missing here? I thought I knew pointers by now. :(
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
search_positions[current_orientation]
不是int**
类型;它的类型是int[6][2]
。search_positions
不是一个指针;它是一个数组。如果您获取数组的地址,则指向
search_positions[current_orientation]
的指针将为int(*)[6][2]
类型:或类型为
int(*)[2]
如果您不获取数组的地址而是让数组到指针的转换发生:search_positions[current_orientation]
is not of typeint**
; it is of typeint[6][2]
.search_positions
is not a pointer; it is an array.A pointer to
search_positions[current_orientation]
, would be of typeint(*)[6][2]
if you take the address of the array:or of type
int(*)[2]
if you don't take the address of the array and instead let the array-to-pointer conversion to take place:指针不是数组,数组不是指针
search_positions
被定义为“由 4 个数组组成的数组,每个数组由 6 个 2int
数组组成”。这使得search_positions[current_orientation]
成为“由 6 个 2int
数组组成的数组”。该数组可以隐式转换为指针,但这只会给您一个指向 2 个
int
数组的指针 (int (*)[2]
)。这与您使用的“指向int
的指针”的类型不同,并且两者之间没有合适的转换。为了解决这个问题,您可以将这些_搜索位置声明为
Pointers are not arrays, arrays are not pointers
search_positions
is defined as an 'array of 4 arrays of 6 arrays of 2int
s'. This makessearch_positions[current_orientation]
an 'array of 6 arrays of 2int
s'.This array can be implicitly converted to a pointer, but that would give you only a pointer to an array of 2
int
s (int (*)[2]
). This is a different type from the 'pointer to pointer toint
' that you were using and there is no suitable conversion between the two.To overcome the problem, you could declare
these_search_positions
as