C中的结构解除引用

时间:2013-07-17 12:25:19

标签: c embedded structure

struct at_response_code
{
    uint8_t *p_string;
    uint8_t event_id;
    uint8_t message_id;
};


struct at_response_code frc_table[] =
{
    (uint8_t*)"OK", EVENT_GSM_ACK_RESPONSE,GSM_MSG_ERROR,
    (uint8_t*)"+CMS ERROR:", EVENT_GSM_ERROR_RESPONSE, GSM_MSG_ERROR,
    (uint8_t*)"+CME ERROR:", EVENT_GSM_ERROR_RESPONSE,GSM_MSG_ERROR,
    (uint8_t*)"ERROR", EVENT_GSM_ERROR_RESPONSE,GSM_MSG_ERROR,
    // array terminator !!!
    (uint8_t*) 0, 0, 0
};

static uint8_t parse_command(uint8_t* p_data, struct at_response_code *table, uint8_t* message_id)
{
    struct at_response_code* p_table;
    uint8_t i = 0;
    uint8_t j;
    p_table = &sms_table[0];
    do
    {
       // j = strlen(&(table->p_string));
        j = strlen((char*)&(table[i].p_string));
        if((memcmp_P(table[i]->p_string, p_data, j)) == 0)
        {
            *message_id = table[i]->message_id;
            return table[i]->event_id;
        }
        i++;
    }
    while(table[i]->p_string != 0);
    return 0;
}

这里我总是得到一个错误作为解除引用错误,或指向不完整类型的指针的下标,当我试图找到使用strlen在结构中定义的字符串的长度...可以任何一个帮助

2 个答案:

答案 0 :(得分:1)

这是错误的,无论它出现在哪里:

table[i]-> ...

table是指向结构(或结构数组)的指针。因此table[i]struct而非指向struct的指针,因此您需要.运算符才能获得其成员。

修改

另一件事,我刚刚注意到你frc_table的初始化者是错的,它需要围绕每个struct初始化者的大括号

struct at_response_code frc_table[] =
{
    { (uint8_t*)"OK", EVENT_GSM_ACK_RESPONSE,GSM_MSG_ERROR },
    { (uint8_t*)"+CMS ERROR:", EVENT_GSM_ERROR_RESPONSE, GSM_MSG_ERROR },
    { (uint8_t*)"+CME ERROR:", EVENT_GSM_ERROR_RESPONSE,GSM_MSG_ERROR },
    { (uint8_t*)"ERROR", EVENT_GSM_ERROR_RESPONSE,GSM_MSG_ERROR },
    // array terminator !!!
    { (uint8_t*) 0, 0, 0 }
};

答案 1 :(得分:0)

由于p_string是指针,因此无需再将其转换为指针。使用以下声明

j = strlen((table[i]->p_string));