Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dereference a pointer to a pointer to a structure

gcc 4.7.2
c89

Hello,

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

Compile error

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,

structure:

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

The way I am calling it:

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;
}
like image 285
ant2009 Avatar asked Jan 14 '23 10:01

ant2009


1 Answers

You have the precedence wrong, it should be e.g.

(*channel)->id
like image 54
Some programmer dude Avatar answered Jan 22 '23 06:01

Some programmer dude