Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c - Address of dereferenced pointer construct

In unqlite c library I found following code:

 pObj = jx9VmReserveMemObj(&(*pVm),&nIdx);

where pVm is:

typedef struct jx9_vm jx9_vm;
jx9_vm *pVm

and function called is declared as:

jx9_value * jx9VmReserveMemObj(jx9_vm *, sxu32 *);

What for construct &(*pVm) is used in call instead of just pVm? Is &(*pVm) equivalent to pVm?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Quoting C11, chapter §6.5.3.2, Address and indirection operators

[...] If the operand is the result of a unary * operator, neither that operator nor the & operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue. [...]

So, yes, they are equivalent.

This construct can be used, however, to check the type of the argument against a pointer type. From the property of unary * operator,

The operand of the unary * operator shall have pointer type.

So, the construct &(*pVm)

  • will be fine, if pvm is a pointer or array name.
  • will generate compiler error, if pvm is a non-pointer type variable.

See the other answer by Alter Mann for code-wise example.

One more difference (in general) is, pVm can be assigned (can be used as LHS of the assignment operator), but &(*pVm) cannot.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...