[Solved] Cast increment and assign in C


[How can I] De-reference and assign a value to a pointer and increment the pointer by (uint64_t) bytes in one line[?]

You are trying to do too many things with a single expression. You should break it up. Moreover, “one line” is more or less arbitrary. I could take your one-liner and spread it out over 12 non-empty lines, and I can take many lines and join them together. The closest actually meaningful thing would be one full expression.

Nevertheless, if you insist on doing it in one full expression, and you’re willing to assume that the size of an int evenly divides the size of a uint64_t, then you might do it like so:

unsigned int *myvalue = 0xDEADBEEF;
*myvalue = 34, myvalue += (sizeof(uint64_t) / sizeof(int));

Of course, that’s mostly sophistry around the meaning of a “full expression” — it would be much better style to use a semicolon instead of a comma operator, but then there would be two full expressions. But that’s the ground on which you chose to pitch your tent.

Alternatively, if you’re open to changing the type of myvalue, then it would be a bit cleaner to do it like this:

uint64_t *myvalue = 0xDEADBEEF;
* (int *) (myvalue++) = 34;

Overall, however, although C permits casting from one object pointer type to another, it makes few promises about how the converted result can safely be used. Furthermore, such casts violate the strict aliasing rule, which may contribute to an optimizing compiler producing an incorrect and hard-to-debug program.

1

solved Cast increment and assign in C