取消对指向结构指针的指针的引用 [英] dereference a pointer to a pointer to a structure

查看:44
本文介绍了取消对指向结构指针的指针的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gcc 4.7.2
c89

你好

我正在尝试取消对指向结构指针的指针的引用,并且在执行以下操作时收到此错误消息:

I am trying to dereference a pointer to a pointer to a structure, and I get this error message when I do the following:

LOG_INFO("CHANNEL ID --- %d", *channel->id);

编译错误

request for member ‘id’ in something not a structure or union

如果我尝试将其转换为正确的指针类型,我仍然收到相同的错误消息:

If I try and cast it to the correct pointer type, I still get the same error message:

LOG_INFO("CHANNEL ID --- %d", (*(channel_t*)channel->id));

我通过声明一个新变量并分配结构指向的地址解决了这个问题:

I solved the problem by declaring a new variable and assigning the address of where the structure is pointing to:

channel_t *ch = NULL;
ch = *channel;
LOG_INFO("CHANNEL ID --- %d", ch->id);

我只是想知道为什么前两种方法失败了.

I am just wondering why the first two methods failed.

非常感谢您的任何建议,

Many thanks for any suggestions,

结构:

typedef struct tag_channel channel_t;
struct tag_channel {
    size_t id;
    char *name;
};

我的称呼方式:

channel_t *channel = NULL;
channel = (channel_t*)apr_pcalloc(mem_pool, sizeof *channel);
LOG_CHECK(job_queue_pop(queue, &channel) == TRUE, "Failed to pop from the queue");

还有这个功能,我遇到了问题:

And the function, I am having trouble with:

apr_status_t job_queue_pop(apr_queue_t *queue, channel_t **channel)
{
    apr_status_t rv = 0;
    channel_t *ch = NULL;

    rv = apr_queue_pop(queue, (void**)channel);
    if(rv != APR_SUCCESS) {
        char err_buf[BUFFER_SIZE];
        LOG_ERR("Failed to pop from the queue %s", apr_strerror(rv, err_buf, BUFFER_SIZE));

        return FALSE;
    }

    ch = *channel;  
    LOG_INFO("CHANNEL ID --- %d", ch->id);
    LOG_INFO("CHANNEL NAME - %s", ch->name);

    return TRUE;
}

推荐答案

您的运算符优先级错误.-> 运算符的优先级高于 . 运算符.所以 -> 之前被评估. 使它成为 *(channel->id) 这是错误的.

You have the operator precedence wrong. -> operator has a higher precedence than . operator. So -> is evaluated before . making it *(channel->id) which is wrong.

看下面的代码.它工作正常.

Look at the following code. It works fine.

typedef struct test_
{
   int i;
}test;

int main()
{
   test a;
   test *aptr = &a;
   test **aptrptr = &aptr;
   a.i=6;
   printf("\n%d\n",(*aptrptr)->i);
   return 0;
}

此处阅读有关优先级的信息.

Read about precedence here.

这篇关于取消对指向结构指针的指针的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆