-3

I am trying to check if an element inside a int array is empty for C language. So I have set my int array as such int numbers[10]; and now I am trying to check if the element is empty by iterating like this

for (int i = 0; i < 9; i++)
{
    if (numbers[i] == ???)
    {
        numbers[i] = 1;
        break; 
   }
}

I would add some number to the element of numbers if I know the element is empty. What should I put in ??? in order do this?

Mardia
  • 37
  • 5

2 Answers2

2

You can't know what's in an uninitialized variable. See this question for some details about uninitialized variables.

You have to provide an initial value for your array. For example:

int numbers[10] = { 0 };

Will initialize all entries to 0.

icebp
  • 1,608
  • 1
  • 14
  • 24
2

I am trying to check if an element inside a int array is empty for C language

There is no value that means "empty" - every possible value of an int represents an integer number.

You know statically that your elements are all uninitialized ... because you didn't initialize them.

(More precisely, they are initialized to indeterminate values, which are permitted to include trap representations, although I'd be very surprised if you encounter one of these for int.)

I would add some number to the element of numbers if I know the element is empty

If in your program you would like to use some int value to mean "empty or currently unused" - you're perfectly welcome to define those semantics.

Just initialize all your elements to INT_MIN or whatever other value you're willing to sacrifice, and then you can compare them to that later.

C doesn't automatically do this for you, because some people want to be able to use the full range of int for representing numbers, are willing to keep track of whether they've initialized things or not, and wouldn't thank the compiler for doing extra work they don't need.


Oh, and for completeness:

What is inside an non-initialized int array?

Nobody knows. Could be anything in there. If you want something specific, set it yourself.

If some value was guaranteed to be there, that could only be because the system promised to always put it there. That's extra work that would be almost always wasted in a well-formed program, so C doesn't do it. Some other languages do.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • When I set my array as 'int number[10];' are all the elements to random integers? I'm not sure what indeterminate values mean here. – Mardia Apr 29 '21 at 10:30
  • It means the value is not determined. "Determined" would mean somebody knows (or decided, or determined) what the value is. Indeterminate means nobody knows what the value is. It's just the dictionary definition. – Useless Apr 29 '21 at 10:37
  • So it would be a random int? I'm sorry for asking trivial stuff :( – Mardia Apr 29 '21 at 10:38
  • No, C doesn't call `rand()` every time you declare a variable with no initializer either. Whatever is in there is wholly determined by a causal chain of events since the computer booted ... it's just that your code doesn't know what that chain of events is. Some compilers, with some switches, _will_ initialize all variables to some special value. But the standard has nothing to say about it, and it won't be the same for every compiler. – Useless Apr 29 '21 at 10:50