Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array initialization in C++ (not strings)

Tags:

c++

I'm playing around with arrays in C++. I defined a 2d array called matrix and am going to extract the negative values and assign it to the array called array.

Is there a way to initialize an array to zero quickly rather than enumerating all the elements? I looked through other postings and lines such as: int array[10] = {} or int array[10] = {0} do not work on my compiler. I get the error message error: variable-sized object ‘array’ may not be initialized if I try using those statements.

My text book said that all arrays are initialized to zero when declared, but I tested this on my compiler and this was not true; I had to force it to zero by using a for-loop. Is there a correct way of doing this?

Oh by the way, I have a mac and use g++ to compile. When I do man g++ it says its a symbolic link to llvm-gcc compiler.

#include<iostream>

const int NROWS = 4, NCOLS = 5;
int matrix[][NCOLS] = {    16,  22,  99,  4, 18, 
                         -258,   4, 101,  5, 98, 
                          105,   6,  15,  2, 45, 
                           33,  88,  72, 16, 3};

int main()
{
    int SIZE = 10;
    int array[SIZE];
    int count=0;

    // Values of array before initalized    
    for(int i = 0; i < SIZE; i++)
    {
        std::cout << array[i] << " ";
    }    
    std::cout << std::endl;

    //Initialize array to zero
    for(int i = 0; i < SIZE; i++)
    {
        array[i]=0;
        std::cout << array[i] << " ";
    }    
    std::cout << std::endl;

    // Extract negative numbers and assign to array
    for(int i = 0; i < NROWS; i++)
    {
        for(int j = 0; j < NCOLS; j++)
        {
            printf("matrix[%i,%i]=%5i\n",i,j,matrix[i][j]);

            if(matrix[i][j] < 0)
            {
                array[count] = matrix[i][j];
                printf("\tarray[%d]= %4d",count, matrix[i][j]);
                printf("\tcount=%d\n", count);
                count++;                
            }
        }

    }

    // Values of array
    for(int i = 0; i < SIZE; i++)
    {
        std::cout << array[i] << " ";
    }

    std::cout << std::endl;

    return 0;
}
like image 229
user1527227 Avatar asked Feb 28 '26 09:02

user1527227


2 Answers

I'm taking a guess here.

int array[10] = {0};

is perfectly legal and should work on any compiler, but I think you tried

int SIZE = 10;
int array[SIZE] = {0};

which is entirely different, and not legal. Array bounds must be constants, not variables.

Some compilers accept variable bounds, but that doesn't make it legal.

like image 130
john Avatar answered Mar 02 '26 00:03

john


Change int SIZE = 10; to const int SIZE=10; or enum{SIZE=10};, and your {} based initialization should work.

You have accidentally used a gcc extension allowing for variable sized arrays.

like image 35
Yakk - Adam Nevraumont Avatar answered Mar 02 '26 00:03

Yakk - Adam Nevraumont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!