开发者

Using a passed by reference struct variable in C

I have a basic pointer question. I have some code like this: Please let me know if anything is wrong in the following code:

struct abc {
  int a;
  int b;
};

void func2(int*); // defined elsewhere

void func1 (struct abc *p1)
{
  struct abc var1 = *p1; // ======> Can I do this ? 

  func开发者_Python百科2(&var1.b);
  func2(&p1->b);      // =========> Which of these 2 is right ? 
}


struct abc var1 = *p1; ======> Can I do this ?

Yes, this copies the struct pointed by p1 in the local variable var1.

func2(&var1.b); func2(&p1->b); =========> Which of these 2 is right ?

Both, if func2() accepts a int* as parameter. It depends if you want func2 to modify p1->b or var1.b.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜