简体   繁体   English

访问另一个结构内的结构数组

[英]Accessing struct array inside another struct

I have a struct named id_item which has 2 elements (id and tipo). 我有一个名为id_item的结构,它具有2个元素(id和tipo)。 I need it this way so I can get back the information of each item at once. 我需要这种方式,以便我可以一次取回每个项目的信息。 Then I have another struct named id_list which has an array of id_item, the total number of elements inserted on it (count) and the a number representing the out position (position). 然后,我有另一个名为id_list的结构,它具有一个id_item数组,在其上插入的元素总数(计数),以及一个表示退出位置(位置)的数字。

There is a function named id_init to initialize the struct, id_insert to insert an element and id_getNext to retrieve next element. 有一个名为id_init的函数初始化该结构,一个id_insert插入一个元素,一个id_getNext检索下一个元素。

I think I'm having problem accessing the id_item array elements inside id_list. 我认为我在访问id_list中的id_item数组元素时遇到问题。 The program compiles just fine but when I run it I don't get what I want. 该程序可以很好地编译,但是当我运行它时,却没有得到想要的东西。 Code and the output is presented bellow. 代码和输出显示在下面。

I have the code bellow inside a library ".h". 我在“ .h”库中有下面的代码。

#define ID_SIZE 30

typedef struct {
    uint8_t id;
    uint8_t tipo;
} id_item;

typedef struct {
    id_item modulo[ID_SIZE];
    uint8_t position;
    uint8_t count;
} id_list;

void id_init(id_list *list) {
    uint8_t i;

    for (i = 0; i < ID_SIZE; i++) {
        list->modulo[i].id = 0x00;
        list->modulo[i].tipo = 0x00;
    }
    list->position = 0;
    list->count = 0;
}

uint8_t id_insert(id_list *list, uint8_t id, uint8_t tipo) {
    if (list->count < ID_SIZE) {
        list->modulo[list->count].id        = id;
        list->modulo[list->count].tipo      = tipo;
        list->count++;
        return 0x06; /*ACK*/
    }
    else {
        // Lista cheia
        return 0x15; /*NAK*/
    }
}

void id_getNext(id_list *list, id_item *modulo) {
    uint8_t pos;

    pos = list->position++;

    if (list->position >= list->count)
        list->position = 0x00;

    modulo = &(list->modulo[pos]);
}

In the main file I do have included the previous library. 在主文件中,我确实包含了以前的库。 After initialization I inserted 30 elements on IDs where the element id is assumed to be the value o "i" and tipo is always the integer "1" and the main code is: 初始化后,我在ID上插入了30个元素,其中元素id假定为值o“ i”,tipo始终为整数“ 1”,主要代码为:

int main()
{
    uint8_t i;
    id_list IDs;
    id_item item;

    id_init(&IDs);

    printf("count: %02d, Position: %02d\n", IDs.count, IDs.position);

    for (i = 0; i < ID_SIZE; i++) {
        printf("%d..\n", i);
        printf("insert: %02X\n",id_insert(&IDs, i, 0x01));
    }
    printf("\n");

    for (i = 0; i < ID_SIZE; i++) {
        id_getNext(&IDs, &item);

        printf("Position: %02d, ID: %d, Tipo: %02d\n", IDs.position, item.id, item.tipo);
    }
    return 0;
}

The program output is showed bellow: 程序输出如下所示:

0..
insert: 06
1..
insert: 06
2..
insert: 06
3..
insert: 06
4..
insert: 06
5..
insert: 06
6..
insert: 06
7..
insert: 06
8..
insert: 06
9..
insert: 06
10..
insert: 06
11..
insert: 06
12..
insert: 06
13..
insert: 06
14..
insert: 06
15..
insert: 06
16..
insert: 06
17..
insert: 06
18..
insert: 06
19..
insert: 06
20..
insert: 06
21..
insert: 06
22..
insert: 06
23..
insert: 06
24..
insert: 06
25..
insert: 06
26..
insert: 06
27..
insert: 06
28..
insert: 06
29..
insert: 06

count: 30, Position: 00
Position: 01, ID: 0, Tipo: 64    
Position: 02, ID: 0, Tipo: 64
Position: 03, ID: 0, Tipo: 64
Position: 04, ID: 0, Tipo: 64
Position: 05, ID: 0, Tipo: 64
Position: 06, ID: 0, Tipo: 64
Position: 07, ID: 0, Tipo: 64
Position: 08, ID: 0, Tipo: 64
Position: 09, ID: 0, Tipo: 64
Position: 10, ID: 0, Tipo: 64
Position: 11, ID: 0, Tipo: 64
Position: 12, ID: 0, Tipo: 64
Position: 13, ID: 0, Tipo: 64
Position: 14, ID: 0, Tipo: 64
Position: 15, ID: 0, Tipo: 64
Position: 16, ID: 0, Tipo: 64
Position: 17, ID: 0, Tipo: 64
Position: 18, ID: 0, Tipo: 64
Position: 19, ID: 0, Tipo: 64
Position: 20, ID: 0, Tipo: 64
Position: 21, ID: 0, Tipo: 64
Position: 22, ID: 0, Tipo: 64
Position: 23, ID: 0, Tipo: 64
Position: 24, ID: 0, Tipo: 64
Position: 25, ID: 0, Tipo: 64
Position: 26, ID: 0, Tipo: 64
Position: 27, ID: 0, Tipo: 64
Position: 28, ID: 0, Tipo: 64
Position: 29, ID: 0, Tipo: 64
Position: 00, ID: 0, Tipo: 64

Process returned 0 (0x0)   execution time : 0.201 s
Press any key to continue.

The expected output would be each id starting from 1 and going to 29 and all tipo being 1, but all id were 0 and all tipo 64 (I don't even know where this 64 came from). 预期的输出将是每个id从1开始到29,所有tipo为1,但是所有id为0和所有tipo 64(我什至不知道该64来自何处)。

Here 这里

modulo = &(list->modulo[pos]);

you are making the local pointer point to the address of list->modulo[pos] , when the function returns, that doesn't affect the passed variable, in fact, after that line you can't alter the passed variable anymore because you overwrite it's address in this statement. 您正在使本地指针指向list->modulo[pos]的地址,当函数返回时,这不会影响传递的变量,实际上,在该行之后,您将无法再更改传递的变量,因为您覆盖此语句中的地址。

It should be 它应该是

*modulo = list->modulo[pos];

or maybe you meant 也许你是说

void id_getNext(id_list *list, id_item **modulo) {
    uint8_t pos;

    pos = list->position++;

    if (list->position >= list->count)
        list->position = 0x00;

    *modulo = &(list->modulo[pos]);
}

and

int main()
{
    uint8_t  i;
    id_list  IDs;
    id_item *item;

    id_init(&IDs);

    printf("count: %02d, Position: %02d\n", IDs.count, IDs.position);

    for (i = 0; i < ID_SIZE; i++) {
        printf("%d..\n", i);
        printf("insert: %02X\n",id_insert(&IDs, i, 0x01));
    }
    printf("\n");

    for (i = 0; i < ID_SIZE; i++) {
        id_getNext(&IDs, &item);

        printf("Position: %02d, ID: %d, Tipo: %02d\n", IDs.position, item->id, item->tipo);
    }
    return 0;
}

which will change the pointer, without copying the whole structure. 这将更改指针,而不复制整个结构。

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

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