开发者

Initialisation from incompatible pointer type warning. Can't find the issue

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 pointe开发者_JAVA技巧r 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.


search_positions[current_orientation] is not of type int**; it is of type int[6][2]. search_positions is not a pointer; it is an array.

A pointer to search_positions[current_orientation], would be of type int(*)[6][2] if you take the address of the array:

int (*these_search_positions)[6][2] = &search_positions[current_orientation];

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:

int (*these_search_positions)[2] = search_positions[current_orientation];


Pointers are not arrays, arrays are not pointers

search_positions is defined as an 'array of 4 arrays of 6 arrays of 2 ints'. This makes search_positions[current_orientation] an 'array of 6 arrays of 2 ints'.
This array can be implicitly converted to a pointer, but that would give you only a pointer to an array of 2 ints (int (*)[2]). This is a different type from the 'pointer to pointer to int' that you were using and there is no suitable conversion between the two.

To overcome the problem, you could declare these_search_positions as

int (*these_search_positions)[2] = search_positions[current_orientation];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜