0

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 ? 

  func2(&var1.b);
  func2(&p1->b);      // =========> Which of these 2 is right ? 
}
2
  • what is the signature of func2()? Commented Aug 23, 2011 at 17:32
  • func2(int *) is the signature. Commented Aug 23, 2011 at 17:36

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

Comments

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.