简体   繁体   English

C:如何使用指针数组生成固定数量的对象

[英]C: How to generate a fixed number of objects with an array of pointers

I would like to create 11 text layers for a pebble watch face. 我想为卵石表盘创建11个文本层。 Without a loop the code would look something like. 没有循环,代码将看起来像。

static TextLayer *time_layer_a;
static TextLayer *time_layer_b; 

... and so on. ... 等等。

How can I do this with a loop and put the pointers to the the objects in a list like structure? 我该如何使用循环将指向对象的指针放在结构之类的列表中

list : in this case array or chain would be a better word because the collection of pointers is for a display with a fixed number of text layers. list :在这种情况下,数组或链条将是一个更好的词,因为指针的集合用于具有固定数量的文本层的显示。 And the number of layers will not be changed during the duration of the program. 并且在程序运行期间不会更改层数。 In C, a list is a structure that can be dynamically resized. 在C语言中,列表是可以动态调整大小的结构。 Using "list like" could mislead helpful people to the assumption that the sought method of chaining is expected to be dynamic. 使用“类似列表”可能会使有用的人误以为所期望的链接方法是动态的。 This is not correct. 这是不正确的。 A structure that uses a fixed allocation of memory is preferred. 使用固定内存分配的结构是首选。

Edit: an array as suggested by John3136 worked perfectly. 编辑:John3136建议的数组可以正常工作。 The array has the added benefit of generating the object pointers with its deceleration. 数组具有通过减速度生成对象指针的额外好处。 And it's a plus that John3136 gave a way to have the code automatically adjust to the size of the array. 而且,John3136提供了一种使代码自动调整为数组大小的方法,这是一个优点。 This is a useful tool to have. 这是一个有用的工具。

Here is the code as applied to create text layers for my watch face. 这是用于为我的表盘创建文本图层的代码。

declarations: 声明:

int i;
static TextLayer* layers[11];

loading method: 加载方式:

// by John3136
// Note the sizeof() stuff means this works unchanged even if you change
// the number of layers.
for(i = 0; i < (short)(sizeof(layers) / sizeof(layers[0])); i++) // (short) converts unsigned interger to +- int
{
  layers[i] = text_layer_create(GRect((bounds.size.w/4)*((i + 1)%4), 
                                      (bounds.size.h/PBL_IF_ROUND_ELSE(5,4))*((i > 2) 
                                                                              ? ((i > 6) 
                                                                                 ? 3 
                                                                                 : 2 ) 
                                                                              : 1), 
                                      (bounds.size.w / 4) ,(bounds.size.h/PBL_IF_ROUND_ELSE(5,4))));
}

unloading method: 卸货方法:

for(i = 0; i < (short)(sizeof(layers) / sizeof(layers[0])); i++) 
{
  text_layer_destroy(layers[i]); 
}

Easiest way that meets your requirements as we know them: An array of 11 pointers to TextLayers. 我们知道,满足您要求的最简单方法:包含11个TextLayers指针的数组。

static TextLayer*  layers[11];

You can then populate with: 然后,您可以填充:

int i;
// Note the sizeof() stuff means this works unchanged even if you change
// the number of layers.
for(i = 0; i < sizeof(layers) / sizeof(layers[0]); i++)
{
    layers[i] = some_func_that_creates_a_layer();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM