|
|
|
|
|
by jstimpfle
765 days ago
|
|
first, yes it did accept the pointer arithmetic even though it issued a warning. Second, the error about implicit conversion from void-pointer to typed-pointer is in C++ only -- not in C, where you won't even get a warning. The error happens because you didn't cast the void pointer to the target type and doesn't have to do anything with the fact that gcc _does_ let you (as a GCC extension) do arithmetic on void pointers. If you remove the "+ 1" you'll still see the same error in C++. In C++, change the target pointer to a void pointer: void *ptr = malloc(42) + 1;
or, alternatively, cast the RHS expression properly (doesn't matter with "+ 1" or without) int *ptr = (int *) (malloc(42) + 1);
and the error will go away. |
|
I was only disputing the claim that ending up with the unaligned ptr is not trivial for given line of code by providing explanation, and counter-example, why this is not possible. At least not in C++ because that's the language I had in mind.
What you're saying is that it's possible to do void pointer arithmetic in C by using the GNU extension - fine, I can't disagree with that.