简体   繁体   English

使用指针访问结构成员

[英]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 . 我是C语言的新手。我试图在main外部创建一个typedef结构,然后创建一个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. 您正在创建一个指向rem_info的指针,并将其地址传递给reset_rem_info。 That means you are passing a pointer to a pointer to a rem_info. 这意味着您将传递一个指向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. 您现在正在处理指向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. 您可能想要的是创建一个实际的rem_info并将其地址(指向rem_info的指针)传递给该函数。

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. 在这里,函数参数需要一个类型为rem_info的指针,而您要传递的是指针的地址,因此存在类型不匹配的情况,因此出现警告。

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. 确保初始化指针,并将指针prem的地址传递给函数参数中指向指针的指针。 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. 谢谢大家的快速回复。.Gabriel的回复给了我很好的见解。 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 ...但是,如果加百列(Gabriel)可以再详细说明一点...好吧,这里是固定代码

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); 如果您想在reset_rem_info中分配prem,您会忘记*为void reset_rem_info(rem_info * prem),然后将其取消引用为bzero,否则不要在reset_rem_info(&prem);写入& reset_rem_info(&prem);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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