가능한 중복 : 나는 갑자기 발견
What is the correct answer for cout << c++ << c;?C++ 이상한 표준 : : cout과 동작을 사용하여 포인터
나는 단지 ouputted 텍스트.
#include <iostream>
int main()
{
int array[] = {1,2,3,4};
int *p = array;
std::cout << *p << "___" << *(p++) << "\n";
// output is 1__1. Strange, but I used brackets! it should be at
// first incremented, not clear.
p = array;
std::cout << *p << "___" << *(++p) << "\n";
// output is 2_2 fine, why first number was affected? I didn't intend
// to increment it, but it was incremented
p=array;
std::cout << *p << "___" << *(p + 1) << "\n";
// output is 1_2 - as it was expected
p = array;
return 0;
}
이러한 동작은 저에게 이상합니다. 그 이유는 무엇입니까?
동일한 표현식에서 인수를 사용하여 증가분을 사용하면 정의되지 않은 동작이 호출됩니다. – Joe
[정의되지 않은 동작 및 시퀀스 포인트] (http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points)를 참조하십시오. –