简体   繁体   中英

accessing structure member using pointers

I am a newbie in C. I am trying to create a typedef struct outside of main and then create a pointer of typedef . Then pass this pointer into another function. However I am getting error. It is driving me crazy .Thank you very much in advance..

typedef struct rem_info
{
    char         ufrag[80];
    char         pwd[80];
    unsigned     comp_cnt;
    pj_sockaddr  def_addr[PJ_ICE_MAX_COMP];
    unsigned     cand_cnt;
    pj_ice_sess_cand cand[PJ_ICE_ST_MAX_CAND];
} rem_info;

void reset_rem_info(rem_info *prem)
{
    pj_bzero(prem, sizeof(rem_info));
}

int main()
{
    rem_info *prem;  
    reset_rem_info(&prem);

    return 0;
}

Error:

*WARNING**:ex7.c:51:1: warning: passing argument 1 of ‘reset_rem_info’ from incompatible pointer type [enabled by default]
 reset_rem_info(&prem);
 ^
ex7.c:41:6: note: expected ‘struct rem_info *’ but argument is of type ‘struct rem_info **’
     void reset_rem_info(rem_info *prem)

Looking at your main function:

int main()
{
    rem_info *prem;  
    reset_rem_info(&prem);

    return 0;
}

You are creating a pointer to rem_info and passing its address to reset_rem_info. That means you are passing a pointer to a pointer to a rem_info. To make it typecheck, you could pass the pointer directly without taking its address.

int main()
{
    rem_info *prem;  
    reset_rem_info(prem);

    return 0;
}

But that will probably give you a bug. You are now dealing with an uninitialized pointer to rem_info. What you probably want is to create an actual rem_info and pass its address (a pointer to rem_info) to the function.

int main()
{
    rem_info prem;  
    reset_rem_info(&prem);

    return 0;
}
void reset_rem_info(rem_info *prem)

Here the function argument expects a pointer of type rem_info and what you are passing is the address of the pointer so there is a type mismatch and hence the warning.

You can have

void reset_rem_info(rem_info **prem)

Make sure you initialize the pointer and pass the address of pointer prem to pointer to pointer in your function argument. Like shown below

int main()
{
   rem_info *prem = malloc(sizeof(rem_info));
   reset_rem_info(&rem_info);
}

or while calling the function have

int main()
{
   rem_info prem;
   reset_rem_info(&prem);
}

So that your function prototype stays unchanged.

Thanks guys for the quick replies..Gabriel response gave me a good insight. However I stuck to Raphael Santos response. ...However, if Gabriel could kindly elaborate the point a bit more please...ok here is the fixed code

typedef struct rem_info
    {
    char         ufrag[80];
    char         pwd[80];
    unsigned     comp_cnt;
    pj_sockaddr  def_addr[PJ_ICE_MAX_COMP];
    unsigned     cand_cnt;
    pj_ice_sess_cand cand[PJ_ICE_ST_MAX_CAND];
    }rem_info;

void reset_rem_info(rem_info *prem)
{
    pj_bzero(prem, sizeof(rem_info));
}

int main()
{

rem_info prem;  
reset_rem_info(&prem);

return 0;
}

The change got rid of the warnings and the segmentation dump...

THANKS A LOT GUYSSS

void reset_rem_info(rem_info **prem)
{
pj_bzero(*prem, sizeof(rem_info));
}

int main()
{
rem_info *prem;  
reset_rem_info(&prem);

return 0;
}

if you want to malloc prem in reset_rem_info you forgot a * for void reset_rem_info(rem_info *prem) then dereference it for bzero else don t write the & in reset_rem_info(&prem);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM