0

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)
3
  • 2
    Try changing reset_rem_info(&prem); to reset_rem_info(prem); You don't need to dereference the pointer. Commented Feb 7, 2015 at 17:07
  • This title "accessing structure member using pointers" is misleading as the question is not about accessing a struct's member but about access a whole struct. Commented Feb 7, 2015 at 17:16
  • I agree...Actually the code at one point is needed to access structure member.However I was stuck before reaching that point.However thanks anyways for your response Commented Feb 7, 2015 at 17:41

4 Answers 4

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1
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.

4 Comments

Although this works, it would change the interface provided by the function. I doubt this is what the OP needs. Also the current code for the function would fail.
@alk OP wants to get rid of the warning what is done in inside the API is something OP has to take care of ..
This approach looks to me like fighting symphtoms ... sort of voodoo programming ... sry.
Take your time and find the proper wording ... or just delete it?
0

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

Comments

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

2 Comments

From what do you conclude: "to malloc prem in reset_rem_info()"?
In your code rem_info* is now malloc so when you will use it it will segfault. So you need to malloc it. When you send a variable in C it make a copy so if you want to malloc rem_info into reset_rem_info you need to send the address of it else if you malloc it using the copy of the adress you will lose it at the end of the function. So to malloc it in the reset_rem_info you have to send the adress and send rem_info **prem and make a *prem = malloc(sizeof(prem));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.