How to access an external block variable in an internal block when the internal block has the same variable declaration?

int main(int argc, char** argv) {
    int i=5;
    {
        int i=7;
        printf("%d\n", i);
    }
    return 0;
}

If I want to access the external value i( int i=5) in printf, then how to do it?

+3
source share
7 answers

Relevant part of C99, section 6.2.1 (Identifier areas):

4 [...] , . , ( ) ( ). , ; , , ( ) .

Update

pmg answer : , , :

int i = 5;
{
    int *p = &i;
    int i  = 7;
    printf("%d\n", *p); /* prints "5" */
}

, .

+8

i , i. -

int i = 5;
{
    int p = i;
    int i  = 7;
    printf("%d\n", p); /* prints "5" */
}
+4

.

+3
source

You cannot access it.

+2
source

I don’t understand why you cannot name “I” and “J”.

Different names for them let you choose.

+1
source

Point to the old ione before defining a new one. (demo at http://ideone.com/dobQX )

But I love the way Jonathan comments on the best!

+1
source

Short answer: you cannot. It is hidden iin the inner area.

0
source

Source: https://habr.com/ru/post/1768432/


All Articles