[c++] C++ error: "Array must be initialized with a brace enclosed initializer"

I am getting the following C++ error:

array must be initialized with a brace enclosed initializer 

From this line of C++

int cipher[Array_size][Array_size] = 0;

What is the problem here? What does the error mean? Below is the full code:

string decryption(string todecrypt)
{
    int cipher[Array_size][Array_size] = 0;
    string ciphercode = todecrypt.substr(0,3);
    todecrypt.erase(0,3);
    decodecipher(ciphercode,cipher);
    string decrypted = "";
    while(todecrypt.length()>0)
    {
        string unit_decrypt = todecrypt.substr(0,Array_size);
        todecrypt.erase(0,Array_size);
        int tomultiply[Array_size]=0;
        for(int i = 0; i < Array_size; i++)
        {
            tomultiply[i] = int(unit_encrypt.substr(0,1));
            unit_encrypt.erase(0,1);
        }
        for(int i = 0; i < Array_size; i++)
        {
            int resultchar = 0;
            for(int j = 0; j<Array_size; j++)
            {
                resultchar += tomultiply[j]*cipher[i][j]; 
            }
            decrypted += char((resultchar%229)-26);
        }
    }
    return decrypted;
}

This question is related to c++ arrays compiler-errors

The answer is


You can't initialize arrays like this:

int cipher[Array_size][Array_size]=0;

The syntax for 2D arrays is:

int cipher[Array_size][Array_size]={{0}};

Note the curly braces on the right hand side of the initialization statement.

for 1D arrays:

int tomultiply[Array_size]={0};

You cannot initialize an array to '0' like that

int cipher[Array_size][Array_size]=0;

You can either initialize all the values in the array as you declare it like this:

// When using different values
int a[3] = {10,20,30};

// When using the same value for all members
int a[3] = {0};

// When using same value for all members in a 2D array
int a[Array_size][Array_size] = { { 0 } };

Or you need to initialize the values after declaration. If you want to initialize all values to 0 for example, you could do something like:

for (int i = 0; i < Array_size; i++ ) {
    a[i] = 0;
}

Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to compiler-errors

intellij idea - Error: java: invalid source release 1.9 Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug' Deserialize JSON with Jackson into Polymorphic Types - A Complete Example is giving me a compile error Android java.exe finished with non-zero exit value 1 error: expected primary-expression before ')' token (C) What does "collect2: error: ld returned 1 exit status" mean? Python3: ImportError: No module named '_ctypes' when using Value from module multiprocessing Maven error :Perhaps you are running on a JRE rather than a JDK? What does a "Cannot find symbol" or "Cannot resolve symbol" error mean? Operator overloading ==, !=, Equals