Questions tagged [array-initialization]

118 questions
786
votes
16 answers

All possible array initialization syntaxes

What are all the array initialization syntaxes that are possible with C#?
Joshua Girard
  • 7,930
  • 3
  • 13
  • 7
117
votes
6 answers

Array initializing in Scala

I'm new to Scala ,just started learning it today.I would like to know how to initialize an array in Scala. Example Java code String[] arr = { "Hello", "World" }; What is the equivalent of the above code in Scala ?
Emil
  • 12,811
  • 17
  • 65
  • 106
80
votes
12 answers

How can I declare a two dimensional string array?

string[][] Tablero = new string[3][3]; I need to have a 3x3 array arrangement to save information to. How do I declare this in C#?
75
votes
8 answers

Array size at run time without dynamic allocation is allowed?

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]…
syaz
  • 2,519
  • 6
  • 35
  • 44
67
votes
7 answers

Create N-element constexpr array in C++11

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}
Christopher Aldama
  • 681
  • 1
  • 6
  • 5
50
votes
6 answers

Braces around string literal in char array declaration valid? (e.g. char s[] = {"Hello World"})

By accident I found that the line char s[] = {"Hello World"}; is properly compiled and seems to be treated the same as char s[] = "Hello World";. Isn't the first ({"Hello World"}) an array containing one element that is an array of char, so the…
halex
  • 15,397
  • 5
  • 50
  • 61
43
votes
3 answers

Initialize List<> with Arrays.asList

Why does this work: String[] array = {"a", "b", "c"}; List list = Arrays.asList(array); but this does not: List list = Arrays.asList({"a","b","c"});
transient_loop
  • 5,144
  • 12
  • 46
  • 95
25
votes
3 answers

Zero-Initialize array member in initialization list

I have a class with an array member that I would like to initialize to all zeros. class X { private: int m_array[10]; }; For a local variable, there is a straightforward way to zero-initialize (see here): int myArray[10] = {}; Also, the class…
22
votes
4 answers

Can array members be initialized self-referentially?

Consider the following code in which we initialize part of D based on another part of D: struct c { c() : D{rand(), D[0]} {} int D[2]; }; int main() { c C; assert(C.D[0] == C.D[1]); } Is the above program well-defined? Can we…
emlai
  • 37,861
  • 9
  • 87
  • 140
21
votes
3 answers

Why can't you use shorthand array initialization of fields in Java constructors?

Take the following example: private int[] list; public Listing() { // Why can't I do this? list = {4, 5, 6, 7, 8}; // I have to do this: int[] contents = {4, 5, 6, 7, 8}; list = contents; } Why can't I use shorthand…
Ethan Turkeltaub
  • 2,771
  • 6
  • 28
  • 45
21
votes
2 answers

What language standards allow ignoring null terminators on fixed size arrays?

We are transitioning C code into C++. I noticed that the following code is well defined in C, int main(){ //length is valid. '\0' is ignored char str[3]="abc"; } as it is stated in Array initialization that: "If the size of the array is…
Trevor Hickey
  • 31,728
  • 22
  • 131
  • 236
20
votes
3 answers

Initializing variable length array

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…
haccks
  • 97,141
  • 23
  • 153
  • 244
19
votes
2 answers

Shortcut for creating character array

Since I like to Split() strings, I usually use new char[] { ';' } or something like that for a parameter for Split(). Is there any shortcut for creating a character array with one element at compile time? Not that I mind typing, but...
Daniel Mošmondor
  • 19,070
  • 12
  • 53
  • 95
14
votes
4 answers

Java Array initialization with type casting

The following code makes me confused: Object[] arr1 = new String[]{"a", "b", "c"}; Object[] arr2 = {"a", "b", "c"}; String[] a = (String[]) arr1; // ok String[] b = (String[]) arr2; //…
Hao Ren
  • 876
  • 1
  • 7
  • 17
14
votes
1 answer

Is there a way to not have to initialize arrays twice?

I need to initialize each element of an array to a non-constant expression. Can I do that without having to first initialize each element of the array to some meaningless expression? Here's an example of what I'd like to be able to do: fn foo(xs:…
kmky
  • 657
  • 4
  • 14
1
2 3 4 5 6 7 8