#include <iostream>
int main() {
char* chars = new char[4];
for(int i = 0; i < 4; ++i)
{
chars[i] = 'a';
}
std::cout << chars;
return 0;
}
That chunk of code works correctly. stdout says
aaaa
However, changing
chars[i] = 'a';
to
*chars++ = 'a';
or
*(chars++) = 'a';
make stdout empty with nothing in stderr. What's going on? I thought the [] was just syntactic sugar for *pointer.
(*chars)++.*(chars + i), but the code has undefined behaviour either way.