warning: uninitialized variable //But I have initialized ! C++ Compiler bug?

后端 未结 5 1220
夕颜
夕颜 2021-01-05 13:56

Iam trying to compile this program but i get warning and when i run vc++ 2010 debugger pops up : ( Here is my code :

#include 
using namespa         


        
5条回答
  •  温柔的废话
    2021-01-05 14:33

    As you say in your comment, yes, you have declared your variables, but you haven't initialized them. Initializing a variable means giving it a value. So in this case, you have told the compiler that you want to create three integers, but you haven't told it what values you want to store in those integers. That would be ok if, for every possible path through your function, index and minn were guaranteed to be given a value, but the problem here is that there is a path through your function where minn and index will never be initialized. First of all, here:

    for(i=0;i

    If you have an array of zeros, then minn is never initialized to a value.

    Then further down:

    for(i=0;imas[i])        
      {
          minn=mas[i];
          index=i;
      }
    

    first of all, if you had an array of zeros, well what is the value in minn? There is no value. You are asking the compiler to compare mas[i] to a number which doesn't exist. Furthermore, what if mas[i] is always equal to zero? Well now you don't initialize minn or index. Yet at the end of the function, you are attempting to use the value of index to get an integer from the array amd then you return minn (which still equals nothing).

    That's the problem you're getting from the compiler. It can see this potential outcome and is warning you that your function can be broken due to these integers never getting a value. To fix it, do what the other lads have suggested and let index and minn equal zero at the start.

提交回复
热议问题