Speedy Learning : The operator & applied to structures in C

ยท

1 min read

When the operator '&' is applied to an object of type 'structure,' it yields a constant pointer whose value corresponds to the address of the first field.

Note that the type of the pointer to the first element and the type of the pointer to the structure differ.

Such as when using:

struct article 
{
    int number, qte; 
    float price;
};
struct article art;

The next simple comparison is illegal because the two pointers do not have the same type:

&art == &art.number; // Incorrect

Note that some compilers merely issue a warning message without interrupting the program's execution. In such cases, the result of the comparison depends on the compiler's implementation.

Nevertheless, the following comparison is legal:

(void *) &art == (void *) &art.number; // Correct and true!
ย