为什么我的C数组代码没有按预期工作?

时间:2021-10-03 19:42:16

I have a pointer in objective c, that called :

我在目标c中有一个指针,称为:

 int *binary[20] ;

which later returned as a pointer .

后来作为指针返回。

When i am trying to assign a number to it with :

当我试图为它分配一个数字:

binary[5]=1;

i get error.

我收到错误。

I know that binary points to an address of the first cell from 20 cells . Why cant i get directly into cell number 5 ?

我知道二进制指向20个单元格中第一个单元格的地址。为什么我不能直接进入5号牢房?

3 个解决方案

#1


2  

The definition int* binary[20] defines an array of 20 pointers to int. Is that really what you want, or did you mean int binary[20] to create an array of 20 ints?

int * binary [20]定义了一个包含20个int指针的数组。这真的是你想要的,还是你的意思是int binary [20]来创建一个20个整数的数组?

Either way, the resulting array is stored on the stack, so that it is an error to return a pointer to this array to some calling code. (After you return to calling code, the stack gets unwound and the memory reserved for your array is marked as free.) If you want to work with the array from calling code, you have to allocate in on the heap:

无论哪种方式,结果数组都存储在堆栈中,因此将指向此数组的指针返回给某些调用代码是错误的。 (在返回调用代码之后,堆栈被解除并且为数组保留的内存被标记为空闲。)如果要在调用代码时使用数组,则必须在堆上分配:

int *binary = calloc(20, sizeof(int));
binary[5] = 1; // fine, even after returning from this function

This gets you a pointer to memory that’s available even after returning from the function. (And the caller bears the responsibility to release that memory using free.)

这将为您提供指向内存的指针,即使从函数返回后也可以使用该指针。 (调用者有责任免费释放内存。)

Anyway, unless you have some specific requirements you should use NSArray or a similar collection class instead. The general rule in Objective-C is to use as high-level APIs as possible, until forced to do otherwise. Allocating an NSArray for several integers is perfectly acceptable in the general case.

无论如何,除非你有一些特定的要求,否则你应该使用NSArray或类似的集合类。 Objective-C中的一般规则是尽可能使用高级API,直到被强制执行。在一般情况下,为几个整数分配NSArray是完全可以接受的。

#2


0  

You have declare a array of pointer.

你已经声明了一个指针数组。

It means you can store 20 memory address in that array.

这意味着您可以在该阵列中存储20个内存地址。

But by binary[5]=1; you are tying to store integer in that instead of address of int.

但是二进制[5] = 1;你想要存储整数而不是int的地址。

You can not store address like static value.

您不能存储静态值等地址。

#3


0  

You declared array of pointers to integer. Your type is int *.

您声明了指向整数的指针数组。你的类型是int *。

If you change the type to int, it will work:

如果将类型更改为int,它将起作用:

int binary[20];
binary[5] = 1;

Btw, your Q has nothing to do with Objective-C, but with C. In Objective-C you are encouraged to work with objects as much as possible. (NSArray and NSValue).

顺便说一下,你的Q与Objective-C无关,但与C有关。在Objective-C中,我们鼓励你尽可能地使用对象。 (NSArray和NSValue)。

#1


2  

The definition int* binary[20] defines an array of 20 pointers to int. Is that really what you want, or did you mean int binary[20] to create an array of 20 ints?

int * binary [20]定义了一个包含20个int指针的数组。这真的是你想要的,还是你的意思是int binary [20]来创建一个20个整数的数组?

Either way, the resulting array is stored on the stack, so that it is an error to return a pointer to this array to some calling code. (After you return to calling code, the stack gets unwound and the memory reserved for your array is marked as free.) If you want to work with the array from calling code, you have to allocate in on the heap:

无论哪种方式,结果数组都存储在堆栈中,因此将指向此数组的指针返回给某些调用代码是错误的。 (在返回调用代码之后,堆栈被解除并且为数组保留的内存被标记为空闲。)如果要在调用代码时使用数组,则必须在堆上分配:

int *binary = calloc(20, sizeof(int));
binary[5] = 1; // fine, even after returning from this function

This gets you a pointer to memory that’s available even after returning from the function. (And the caller bears the responsibility to release that memory using free.)

这将为您提供指向内存的指针,即使从函数返回后也可以使用该指针。 (调用者有责任免费释放内存。)

Anyway, unless you have some specific requirements you should use NSArray or a similar collection class instead. The general rule in Objective-C is to use as high-level APIs as possible, until forced to do otherwise. Allocating an NSArray for several integers is perfectly acceptable in the general case.

无论如何,除非你有一些特定的要求,否则你应该使用NSArray或类似的集合类。 Objective-C中的一般规则是尽可能使用高级API,直到被强制执行。在一般情况下,为几个整数分配NSArray是完全可以接受的。

#2


0  

You have declare a array of pointer.

你已经声明了一个指针数组。

It means you can store 20 memory address in that array.

这意味着您可以在该阵列中存储20个内存地址。

But by binary[5]=1; you are tying to store integer in that instead of address of int.

但是二进制[5] = 1;你想要存储整数而不是int的地址。

You can not store address like static value.

您不能存储静态值等地址。

#3


0  

You declared array of pointers to integer. Your type is int *.

您声明了指向整数的指针数组。你的类型是int *。

If you change the type to int, it will work:

如果将类型更改为int,它将起作用:

int binary[20];
binary[5] = 1;

Btw, your Q has nothing to do with Objective-C, but with C. In Objective-C you are encouraged to work with objects as much as possible. (NSArray and NSValue).

顺便说一下,你的Q与Objective-C无关,但与C有关。在Objective-C中,我们鼓励你尽可能地使用对象。 (NSArray和NSValue)。