Hi there,

I know this question is very common but I still didn't understand that why we can't check if array is not initialized. Isn't there a way to check an array is initialzed by any values(int, double).

I am working on array based program, in which I have to populate array, print array, search item in array etc. I need to check that if array is not initalized by values user can't print, search values in array.

    do
    {
        cout<<"Press 1 to populate array."<<endl;
        cout<<"Press 2 to print array."<<endl;
        cout<<"Press 3 to sort array."<<endl;
        cout<<"Press 4 to search key in array."<<endl;
        cout<<"Press 5 to delete number in array."<<endl;

        menuNo = getch();
        cout<<""<<endl;

        switch(menuNo)
        {
            case '1':
            {
                popArr(arr, size);
            }
            break;

            case '2':
            {
                printArr(arr, size);
            }
            break;

            case '3':
            {
                sortArr(arr, size);
            }
            break;

            case '4':
            {
                searchKey(arr, size);
            }
            break;

            case '5':
            {
                deleteKey(arr, size);
            }
            break;

            default:
            {
                cout<<"Invalid menu number !"<<endl;
            }
            break;
        }

        cout<<"Do you want to do another task(y/n) ??"<<endl;
        repeat = getch();

        cout<<""<<endl;

    }while(repeat == 'y' || repeat == 'Y');

I need array based solution. I'll highly appreciate this.

Create a flag, if someone enters something the flag is set and you can use the print function.

If not, it remains disabled.

why we can't check if array is not initialized.

How can you tell the difference between a byte that has been "initialised" and a byte that has not been "initialised", just by looking at the byte? It's meaningless.

Isn't there a way to check an array is initialzed by any values(int, double).

Not without some scaffolding in place to help. Consider an array of int. Regardless of whether the array is initialized or not, all elements contain an integer value. The difference is that an an uninitialized array those values are meaningless to you. Unless your compiler helpfully initializes all data with something predictable you can test against (not a safe assumption), there's no way to check for an uninitialized variable given only that variable.

You might consider wrapping your array in a class that includes that scaffolding or ensures that the array is always initialized.

p.s. It's technically undefined behavior to read an uninitialized variable's value. So even if your compiler gives you predictable defaults, your code isn't portable.

Thanks for your prompt responses !

But I didn't understand too much technical things. Please can anyone explain in simple way and give a solution...

The simplest solution is size. size should represent the number of items added to the array, so if size is 0, there are no items and the array hasn't been "initialized".

Also include a capacity parameter so that your functions know how many items the array could potentially hold as well.

This article has been dead for over six months. Start a new discussion instead.