Introduction
Recently, they approached me with the question, βwhat is it and how to use it?β, Showing the following code:
extern "C" { void byteMaskDowngrade(byte***const byteMask, const byte *const *const && source) {
This colleague was my work colleague, and we did not immediately understand what exactly the parameters mean in the function declaration (for those who are interested where such an announcement might be needed: in cryptography).
And in anticipation of possible problems from colleagues in the workshop, I decided to create this article, putting it as a cheat sheet, namely the answers to two questions:
- How to write such ads here?
- And how to read them correctly?
"*"
The case with one asterisk is the most common, however, misunderstandings are also possible here:
- How to:
- So:
const byte p;
- Or so:
byte const p;
- Or in the case of the entry '*':
- So:
byte *const p;
- Or so:
const byte *p;
Strictly speaking, from the point of view of the compiler, the presence of an asterisk and its position in the expression raises the question since the final meaning of the entry will be different. The presence of the const specifier in the above case does not make any difference when taking into account its position relative to the data type, however, the situation changes dramatically when pointers appear: one, two, three ... well, you understand:
void p() {
All this looks more interesting when the second pointer appears (here an efficient reading rule for writing is already beginning to be traced):
void pp() {
As you can see, a double pointer can be represented in as many as 8 different ways, each of which defines a special way of using the target data.
The rules for reading such expressions are as follows:
- look for the '=' sign in the expression and read the expression from right to left;
- skip the name of the variable;
- further meetings either '*', which means a regular pointer, or '* const' - a constant pointer;
- thus, we read until a data type (byte) is encountered;
- and the last word to the left of the data type can be const, the presence of which means that this whole construction refers to data that cannot be changed; if const is not, then it is possible.
This form of writing and reading makes it easy to read and understand even the most sophisticated expressions)
Here's an example of a
complete set of expressions with a triple pointer:
"***"
void ppp() {