Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point of declaration in C++

Tags:

c++

Why isn't the output 101 while I assigned the previous x to the new x?

int x = 101;
{
    int x = x;
    std::cout << x << std::endl;
}

Output (garbage):

422634

I thought the second x would be initialized to 101 but it isn't initialized.

Note: The solution in this case is int x = ::x but the question is why it happens.

like image 206
masoud Avatar asked Apr 01 '13 15:04

masoud


People also ask

What is the use of declaration in C?

A "declaration" establishes an association between a particular variable, function, or type and its attributes. Overview of Declarations gives the ANSI syntax for the declaration nonterminal. A declaration also specifies where and when an identifier can be accessed (the "linkage" of an identifier).

What is the pointer declaration in C?

A pointer declaration names a pointer variable and specifies the type of the object to which the variable points. A variable declared as a pointer holds a memory address.

What is declaration & definition in C?

A declaration means (in C) that you are telling the compiler about type, size and in case of function declaration, type and size of its parameters of any variable, or user-defined type or function in your program. No space is reserved in memory for any variable in case of the declaration.

What is declaration with example?

The definition of a declaration is a formal announcement. An example of a declaration is a government's statement about a new law. noun. 4. A list of items for various legal purposes, e.g. customs declaration.


2 Answers

Point of declaration

The point of declaration for a name is immediately after its complete declarator and before its initializer... [C++ Standard § 3.3.2/1]

Compiler completes the declaration when it knows enough about declarator.

Above code is equal to the below one:

int x = 101;
{
  int x;
  x = x; <------------------// Self assignment, assigns an indeterminate value.
  std::cout << x << std::endl;
}

Because, the declaration of inner x completed before = (assignment)

int x = x; <--// Now, we have the new `x` which hides the older one, 
     ^        // so it assigns itself to itself
     |
     +---// Point of declaration,
         // here compiler knows everything to declare `x`.
         // then declares it.

 

On the other hand, when we declaring complex objects, the point of declaration is farther. So, the behavior is different.

For example, below code is OK

const int i = 2;
{
  int i[i];
         ^
         |
         +----// Point of declaration
              // compiler has to reach to "]"
              // therefore before declaring `i` as an array
              // there is just one `i`, the `i` of `const int i=2`
}

In above code, compiler has to know the actual size of the array to complete the declaration, so the point of declaration is ]. Therefore the i within [i] is the outer i because declaration of the i of int i[... isn't completed yet. Thus, it declares an array with 2 elements (int i[2];).

 

Also, this example shows the point of declaration for an enumerator

const int x = 12;
{
  enum { x = x };
               ^
               |
               +---// Point of declaration
                   // compiler has to reach to "}" then
                   // there is just one `x`, the `x` of `const int x=12`

}

The enumerator x is initialized with the value of the constant x, namely 12.

like image 182
masoud Avatar answered Oct 21 '22 23:10

masoud


There's another way to do it.

#include <iostream>
int x = 101;
int main()
{
  int x = ::x;
  std::cout << x << std::endl;
  std::cin.get();
}
like image 9
greego Avatar answered Oct 21 '22 23:10

greego