Search code examples
cpointersdouble-pointeraddressof

How to use address-of operator along with prefix increment on pointers in the same statement?


Is it possible to use address-of operator alongside prefix increment on pointers in the same statement, if yes how?

Example,

#include <stdio.h>
#include <stdint.h>

void main() {
    uint8_t arr_var[2];
    arr_var[0] = 0xa;
    arr_var[1] = 0xf;
    uint8_t *ptr = arr_var;
    uint8_t **dptr = &(++ptr);
}

Im getting the error

error: lvalue required as unary '&' operand

uint8_t **dptr = &(++ptr);

Is there any other alternatives rather than making it 2 separate statements (increment (ptr++) and then address-of (&ptr)).


Solution

  • It seems I was thrown off by one difference between C and C++...

    In C the result of the increment or decerement operators is never an lvalue, and you can only get the addresses of lvalues.

    This increment/decrement reference explicitly include the example &++a and says it's invalid.

    To get a pointer to ptr you must use plain &ptr. Before of after incrementing the pointer doesn't matter, as dptr will be a pointer to ptr itself.