声明动态数组
问题描述:
如何声明动态数组?例如:
How can I declare dynamic array? For example:
int k=5;
我想要一个如下所示的数组:
I want to have an array like below:
int myArray[k];
答
有时确实需要真正的数组(不是 NSArray).参见例如 indexPathWithIndexes:length: 在 NSIndexPath 中,它以整数数组作为参数.对于数组分配,您应该使用以下方法:
Sometimes true arrays (not NSArray) are really needed. See for example indexPathWithIndexes:length: in NSIndexPath, it take array of uintegers as parameter. For array allocation you should use the following approach:
NSUInteger *arr = (NSUInteger*)malloc(elementsCount * sizeof(NSUInteger) );
arr[0] = 100;
free(arr);