Pointer and its use: * & p and & * p? are they legal?

Hi, I'm new to C programming. Am I trying to figure out what * & p and & * p-pointer mean? Does it ever help?

q=*&p
+4
source share
1 answer

They are not operations, except that they can cause compile-time errors. Therefore, they can be useful in the macro as a statement. *&lguarantees that it lis an lvalue, but &*pguarantees that the p(implicitly convertible) pointer.

#define ASSERT_LVALUE(l)  (void)(*&(l))
#define ASSERT_POINTER(p) (void)(&*(p))

int main(void) {
    int* p;
    int i;

    ASSERT_LVALUE(i);
    ASSERT_LVALUE(3);   /* error: lvalue required as unary ‘&’ operand */

    ASSERT_POINTER(p);
    ASSERT_POINTER(i);  /* error: invalid type argument of unary ‘*’ (have ‘int’) */
    ASSERT_POINTER(3);  /* error: invalid type argument of unary ‘*’ (have ‘int’) */

    return 0;
}
+3
source

Source: https://habr.com/ru/post/1687397/


All Articles