c++ unassigned array

int array[5]

what if I didn't assign any values to array[4] before I access it? what happens when i access it?

it is possible to check if an entry is assigned?
TroudeloupAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
>> what if I didn't assign any values to array[4] before I access it? what happens when i access it?
It depends. If it's a global variable it'll automatically be initialized to 0; else it'll contain random data.

>> it is possible to check if an entry is assigned?
Not really, no. Always initialize your data. The code below will set every element in the array to be 0.
int array[5] = {0};
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
More in initializing arrays can be found here: http://www.cplusplus.com/doc/tutorial/arrays.html
0
evilrixSenior Software Engineer (Avast)Commented:
Additionally, since you are using C++ you might want to consider using std::vector<int> instead.
http://www.cplusplus.com/reference/stl/vector/
0
peetmCommented:
Exactly as evilrix says ... you could also set the values in this array (as it's an int array) using memset, e.g.

    memset(array, -1, sizeof(array));

That will set each member's value to -1 - which you could use as indicating an item's not been assigned to, if -1 isn't a valid value.  The value (-1 here) is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.