티스토리 뷰
int arr[n] 처럼 배열의 크기를 n, 즉 변수로 선언할수 있을까?
이전부터 c++에서는 안된다고는 알고있었는데 한번 제대로 찾아봤다.
결론은 c++ 에서는 원래는 허용을 안하지만, 일부 컴파일러에서는 허용한다고 한다.
그말은 결국 이런식으로 선언하면 안된다는 말이다.
그렇다면 이런식으로 배열의 크기를 변수로 선언하고 싶으면 어떤 방법이 있을까
1. 동적으로 할당
int n = 10;
int* a = new int[n];
2. 가변적 컨테이너 사용 (vector 등)
int n = 10;
vector<int> a(n);
3. constant(상수) 사용
const int n = 10;
int a[n];
참고:
Array[n] vs Array[10] - Initializing array with variable vs real number
I am having the following issue with my code: int n = 10; double tenorData[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Returns the following error: error: variable-sized object 'tenorData' may not...
stackoverflow.com
'노트' 카테고리의 다른 글
| c++) next_permutation, prev_permutation (0) | 2021.04.30 |
|---|---|
| clion, 한글 주석시 컴파일 오류 (0) | 2021.04.30 |
| c++) 입출력 관련 속도 (0) | 2021.04.14 |
| c++) 문자열 관련 함수들 저장 (0) | 2021.04.02 |
| c++) 배열 초기화, memset vs fiil_n (0) | 2021.04.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- back tracking
- two pointer
- binary search
- Implementation
- Kruskal
- db
- MVC
- priority queue
- floyd warshall
- 이분탐색
- graph
- Python
- 조합
- Unity
- BFS
- DP
- CSS
- Spring
- dfs
- Dijkstra
- 자료구조
- permutation
- Brute Force
- C++
- recursion
- greedy
- Stack
- Tree
- C
- 재귀
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
글 보관함
