开发者

Using a passed by reference struct variable in C

开发者 https://www.devze.com 2023-03-30 08:16 出处:网络
I have a basic pointer question. I have some code like this: Please let me know if anything is wrong in the following code:

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

精彩评论

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