简体   繁体   中英

Accessing struct array inside another struct

I have a struct named id_item which has 2 elements (id and 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).

There is a function named id_init to initialize the struct, id_insert to insert an element and id_getNext to retrieve next element.

I think I'm having problem accessing the id_item array elements inside id_list. 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".

#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:

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).

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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