arrayscpointersstructurealloc

Pointers,structure C


How can I work with the structure if i pass structure like parameter function ** ?

typedef struct TQueue
{
    ...
    int m_Len;
}TQUEUE;

void nahraj(TQUEUE **tmp)
{
    tmp[5]->m_Len = 7;
}

int main (void)
{
    TQUEUE *tmp;
    tmp = malloc(10*sizeof(*tmp));
    nahraj (&tmp);
    printf("%d\n",tmp[5].m_Len);
}

Solution

  • You need to dereference tmp before indexing it, since it's a pointer to an array, not an array itself.

    And the elements of the array are structures, not pointers to structures, so you use . rather than ->.

    void nahraj(TQUEUE **tmp)
    {
        (*tmp)[5].m_Len = 7;
    }