Computer Science/C++
[C++] Chapter 2: Types
focalpoint
2023. 8. 21. 23:53
1. Function types
a) return type: int, arg type: double (function)
int func(double)
int (double)
b) return type: &int[2][3], arg type: double (function)
int (*func(double))[2][3]
int (*(double))[2][3]
c) return type: function pointer of (b), arg type: double (function pointer)
int (*(*)(double))[2][3]
d) return type: int[2][3], arg type: vector<double> (function)
int (std::vector<double>)[2][3]
2. typedef
typedef int(*ARRAY2D_POINTER)[2][3];
above defines "ARRAY2D_POINTER" as int* [2][3]
3. Pointer
void main() {
int* arr[2];
int x = 1;
int y = 2;
arr[0] = &x;
arr[1] = &y;
cout << *arr[0] << endl;
int** brr = new int*[2];
brr[0] = &x;
brr[1] = &y;
cout << *brr[0] << endl;
delete[] brr;
}
4. const
void main() {
// all elements are const
const int arr[3] = { 1, 2, 3 };
// *cp is const
int a = 3;
const int* cp;
cp = &a;
// cp is const
int* const cp = &a;
}
5. auto
void main() {
vector<int> v = { 1, 2, 3 };
int sum = 0;
// auto : vector<int>::iterator
for (auto it = v.begin(); it != v.end(); it++) {
sum += *it;
}
cout << sum << endl;
}