array-initialization

Initializing variable length array [duplicate]

只愿长相守 提交于 2019-11-26 15:28:57
This question already has an answer here: C compile error: “Variable-sized object may not be initialized” 7 answers C error “variable-sized object may not be initialized” [duplicate] 3 answers gcc complains: variable-sized object may not be initialized 3 answers On initializing a Variable length array compiler gives an error message: [Error] variable-sized object may not be initialized Code snippet: int n; printf("Enter size of magic square: "); scanf("%d",&n); int board[n][n] = {0}; How should Variable Length arrays be initialized? And why it's all elements are not initialized to 0 in the way

Create N-element constexpr array in C++11

a 夏天 提交于 2019-11-26 12:16:19
Hello i'm learning C++11, I'm wondering how to make a constexpr 0 to n array, for example: n = 5; int array[] = {0 ... n}; so array may be {0, 1, 2, 3, 4, 5} In C++14 it can be easily done with a constexpr constructor and a loop: #include <iostream> template<int N> struct A { constexpr A() : arr() { for (auto i = 0; i != N; ++i) arr[i] = i; } int arr[N]; }; int main() { constexpr auto a = A<4>(); for (auto x : a.arr) std::cout << x << '\n'; } Kal Unlike those answers in the comments to your question, you can do this without compiler extensions. #include <iostream> template<int N, int... Rest>

How to create an array when the size is a variable not a constant?

心已入冬 提交于 2019-11-26 07:43:56
问题 I\'ve got a method that receives a variable int. That variable constitutes an array size (please, don\'t offer me a vector). Thus, I need to init a const int inside my method to initialize an array of specific size. Question: how do I do that? void foo(int variable_int){ int a[variable_int] = {0}; //error } 回答1: int *a = new int[variable_int]; Remember to delete[] the allocated space when you are done with it! 回答2: You asked for a non-vector solution but let's go over it because you might

Create N-element constexpr array in C++11

前提是你 提交于 2019-11-26 03:34:00
问题 Hello i\'m learning C++11, I\'m wondering how to make a constexpr 0 to n array, for example: n = 5; int array[] = {0 ... n}; so array may be {0, 1, 2, 3, 4, 5} 回答1: In C++14 it can be easily done with a constexpr constructor and a loop: #include <iostream> template<int N> struct A { constexpr A() : arr() { for (auto i = 0; i != N; ++i) arr[i] = i; } int arr[N]; }; int main() { constexpr auto a = A<4>(); for (auto x : a.arr) std::cout << x << '\n'; } 回答2: Unlike those answers in the comments

Array size at run time without dynamic allocation is allowed?

做~自己de王妃 提交于 2019-11-25 22:30:38
问题 I\'ve been using C++ for a few years, and today I saw some code, but how can this be perfectly legal? int main(int argc, char **argv) { size_t size; cin >> size; int array[size]; for(size_t i = 0; i < size; i++) { array[i] = i; cout << i << endl; } return 0; } Compiled under GCC. How can the size be determined at run-time without new or malloc ? Just to double check, I\'ve googled some and all similar codes to mine are claimed to give storage size error. Even Deitel\'s C++ How To Program p.

All possible array initialization syntaxes

北城以北 提交于 2019-11-25 22:26:20
问题 What are all the array initialization syntaxes that are possible with C#? 回答1: These are the current declaration and initialization methods for a simple array. string[] array = new string[2]; // creates array of length 2, default values string[] array = new string[] { "A", "B" }; // creates populated array of length 2 string[] array = { "A" , "B" }; // creates populated array of length 2 string[] array = new[] { "A", "B" }; // created populated array of length 2 Note that other techniques of