来自不兼容指针类型警告的初始化。找不到问题

发布于 2024-10-09 14:59:40 字数 645 浏览 5 评论 0原文

我假设此警告会使我的应用程序崩溃。我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身软味 2024-10-16 14:59:40

search_positions[current_orientation] 不是 int** 类型;它的类型是int[6][2]search_positions 不是一个指针;它是一个数组。

如果您获取数组的地址,则指向 search_positions[current_orientation] 的指针将为 int(*)[6][2] 类型:

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

或类型为 int(*)[2] 如果您不获取数组的地址而是让数组到指针的转换发生:

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

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];
荒岛晴空 2024-10-16 14:59:40

指针不是数组,数组不是指针

search_positions 被定义为“由 4 个数组组成的数组,每个数组由 6 个 2 int 数组组成”。这使得 search_positions[current_orientation] 成为“由 6 个 2 int 数组组成的数组”。
该数组可以隐式转换为指针,但这只会给您一个指向 2 个 int 数组的指针 (int (*)[2])。这与您使用的“指向 int 的指针”的类型不同,并且两者之间没有合适的转换。

为了解决这个问题,您可以将这些_搜索位置声明为

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文