在另一个struct中访问struct数组

时间:2015-04-12 16:55:36

标签: c function pointers struct

我有一个名为id_item的结构,它有2个元素(id和tipo)。我需要这样,所以我可以立即取回每个项目的信息。然后我有另一个名为id_list的结构,它有一个id_item数组,插入的元素总数(count)和一个表示out位置(位置)的数字。

有一个名为id_init的函数用于初始化struct,id_insert用于插入元素,而id_getNext用于检索下一个元素。

我认为我在访问id_list中的id_item数组元素时遇到问题。该程序编译得很好但是当我运行它时我不能得到我想要的东西。代码和输出如下所示。

我的代码低于图书馆" .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]);
}

在主文件中,我确实包含了以前的库。在初始化之后,我在ID上插入了30个元素,其中元素id被假定为值o&#34; i&#34;和tipo总是整数&#34; 1&#34;主要代码是:

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;
}

程序输出显示如下:

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.

预期输出将是每个id从1开始并且到29并且所有tipo都是1,但是所有id都是0并且所有tipo 64(我甚至不知道这64来自哪里)。

1 个答案:

答案 0 :(得分:4)

下面

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

你正在使本地指针指向list->modulo[pos]的地址,当函数返回时,它不会影响传递的变量,事实上,在该行之后你无法改变因为你在这个陈述中覆盖它的地址,所以再传递变量。

应该是

*modulo = list->modulo[pos];

或者你的意思是

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]);
}

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;
}

将更改指针,而不复制整个结构。