Questions tagged [array-initialization]
123 questions
                    
                    878
                    
            votes
                
                19 answers
            
        All possible array initialization syntaxes
What are all the array initialization syntaxes that are possible with C#?
         
    
    
        Joshua Girard
        
- 8,850
- 3
- 15
- 7
                    123
                    
            votes
                
                7 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
        
- 13,577
- 18
- 69
- 108
                    87
                    
            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#?
        delete
                    82
                    
            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,659
- 6
- 36
- 44
                    81
                    
            votes
                
                9 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
        
- 821
- 1
- 7
- 5
                    55
                    
            votes
                
                4 answers
            
        Are char arrays guaranteed to be null terminated?
#include 
int main() {
    char a = 5;
    char b[2] = "hi"; // No explicit room for `\0`.
    char c = 6;
    return 0;
}
Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us,… 
         
    
    
        Dan
        
- 2,694
- 1
- 6
- 19
                    51
                    
            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
        
- 16,253
- 5
- 58
- 67
                    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,984
- 15
- 58
- 117
                    33
                    
            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…
         
    
    
        ValarDohaeris
        
- 6,064
- 5
- 31
- 43
                    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…
         
    
    
        Emil Laine
        
- 41,598
- 9
- 101
- 157
                    22
                    
            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,718
- 12
- 58
- 99
                    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,931
- 8
- 30
- 45
                    21
                    
            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
        
- 104,019
- 25
- 176
- 264
                    20
                    
            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
        
- 36,288
- 32
- 162
- 271
                    19
                    
            votes
                
                2 answers
            
        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
        
- 783
- 6
- 17