Home  >  Q&A  >  body text

关于c++变量作用域问题-在一个for循环体中定义的变量-外部怎么用

高洛峰高洛峰2765 days ago846

reply all(5)I'll reply

  • 阿神

    阿神2017-04-17 14:21:26

    First of all, to answer your question: variables defined inside the loop (that is, inside the curly braces) cannot be used outside. And when compiling, you will be told that this variable is not defined.
    Cause: Using an internally defined variable outside the curly braces has exceeded the scope of the variable.
    The following is a brief introduction to C/C++ curly brace scope

    Cure brace scope

    //作用域,可以看作一个变量的有效使用区域
    
    //函数外部也是一个大的作用域,暂且记作:作用域0
    void Fun()
    {
        //函数内部作用域,暂且记作:作用域1
        
        while()
        {
            //循环内部,暂且记作:作用域2
             if()
            {
                //if内部,也是一个作用域,暂且记作:作用域3
            }
        }
        
        if()
        {
            //if内部,也是一个作用域,暂且记作:作用域4
        }
        
        {
            //我只写个花括号,这个也是一个作用域,暂且记作:作用域5
        }
    }

    From the above code block, you can see that
    scope 0 directly includes scope 1, and at the same time, indirectly includes scopes 2, 3, 4, and 5; (variables declared in scope 1, It can be used in itself, as well as in 2, 3, 4, and 5, but it cannot be used in the superior scope of 1, that is, scope 0, outside the function)
    Scope 1, which directly includes scope 2 and 4. , 5, and at the same time, it indirectly includes scope 3; (Variables declared in scope 2 can be used in scope 2 and scope 3, the subordinate scope of 2, but cannot be used in 0, 1, 4, and 5 . )
    The use of variables can only extend downward from the scope where the variable is declared, not in the reverse direction, and cannot go up to the upper scope. In other words, it can only be used under the current scope or sub-scope.

    Location of variable declaration

    Next, let’s briefly talk about the variable declaration in C/C++.
    In the standard C, there are strict specifications that the declaration of variables must be at the beginning of the scope. (If you have any questions about the code, it may be that you have confused C in C++, or the standard of your compiler is not strict. The strict standard should generally be the C standard under linux or unix.)
    Even, everything inside the function Variable declarations are artificially strictly required to be at the beginning of the function to prevent variables from being declared and defined everywhere. (This may be different from what is described in your teacher or master, as well as in some books and videos. That is because most programming languages ​​​​no longer require this and they have confused the rules.) Please remember that strict In standard C, variables are declared in the scope or at the beginning of the function. My personal suggestion is to put them all at the beginning of the function.
    Advantages

    1. In the development stage, some function bodies are generally very large (in this case, it is usually a function design problem, but it does not rule out necessity.) Faced with such a huge function body, the declaration of variables must be unified At the beginning of the function, you can prevent variables from being undeclared when used. Especially when using goto statements, it is easy to skip the declaration of certain variables.

    2. Facilitates multiple effective use of variables, that is, once declared, multiple initializations, and multiple uses.

    3. Easy to read..

    The above is my humble experience and I would like to share it here. Please correct me if there are any errors. There are some things that you will understand the more you write about them.

    reply
    0
  • PHPz

    PHPz2017-04-17 14:21:26

    can only be defined outside the loop body. If you don’t want to expose z outside the loop body, you can wrap two loop bodies with {} and define {}z in

    reply
    0
  • PHPz

    PHPz2017-04-17 14:21:26

    I don’t know if there is any way to use the variables defined in the previous loop in another loop, and it is not recommended.
    If there are more codes, it will be messy

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:21:26

    Let the variable z be used in the next loop?

    First let me talk about my understanding of the use of variables
    Variables actually correspond to a space in the memory. The bottom layer of the machine does not know what variable names there are, and some are just memory addresses.
    We use variables to either get values ​​or assign values, that is, to read and write memory

    {
        int z = 0;  // 假设 z 对应的内存为 A
    }
        z = 1; // 这里z能不能使用? 这里能不能往A中写1
               // 答案是否定的
        //虽然你感觉两个变量都是z,他们应该代表的是同一个内存
        //但,实际上面代码和下面代码是等同的  
    {
        int z = 0;  
    }
        a = 1;      

    reply
    0
  • 黄舟

    黄舟2017-04-17 14:21:26

    No, it violates the rules

    reply
    0
  • Cancelreply