Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int& foo() {
- int a = 10;
- return a;
- }
- void decay_to_pointer(int arr[3]) {
- std::cout << "array decays to pointer: " << sizeof(arr) << std::endl;
- }
- void pass_by_reference(int (&arr)[3]) {
- std::cout << "array passed to function by reference: " << sizeof(arr) << std::endl;
- }
- int main() {
- int b = 10;
- double c = 4.5;
- double* dptr;
- // dptr = &b // compile error
- dptr = reinterpret_cast<double*>(&b);
- std::cout << "before b: " << b << std::endl;
- std::cout << "before *dptr: " << *dptr << std::endl; // 이상한 주소를 가리키게 됨
- *dptr += 1;
- std::cout << "before b: " << b << std::endl;
- std::cout << "before *dptr: " << *dptr << std::endl;
- std::cout << std::endl;
- int* iptr;
- // iptr = &c; // compile error
- iptr = reinterpret_cast<int*>(&c);
- std::cout << "before c: " << c << std::endl;
- std::cout << "before iptr: " << *iptr << std::endl; // 이상한 주소를 가리키게 됨
- *iptr += 2;
- std::cout << "before c: " << c << std::endl;
- std::cout << "before iptr: " << *iptr << std::endl;
- std::cout << std::endl;
- int* dang_p;
- // int* dang_p = 10; // rvalue assign -> compile error
- // *dang_p = 10; // runtime error
- int& dang_r = foo();
- // std::cout << *dang_p << std::endl; // runtime error
- // std::cout << dang_r << std::endl; // runtime error
- int arr[3] = {0,};
- decay_to_pointer(arr);
- pass_by_reference(arr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement