开发者

Dynamically size array?

Super quick question, my initial thought 开发者_JAVA百科was this is not going to work, but then I thought why not give it a go. Now I am thinking it won't work as the resultant array does not seem to be properly formed? My question, should this work?

NSUInteger numPoints = [[[self dataModel] locationFake] count];
CLLocationCoordinate2D points[numPoints];


No, it wouldn't work as the points[] array needs to be sized statically. That is, the compiler needs to know the size of that array, but can't possibly know it until runtime.

If you change it to:

CLLocationCoordinate2D *points = malloc(numPoints * sizeof(CLLocationCoordinate2D));

That should work. Just don't forget to free() it later when you are done.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜