Is C assigned a stack for every {}?

Asked 2 years ago, Updated 2 years ago, 77 views

void foo() {
   int c[100];
   {
       int d[200];
   }
   //d[0]=1; //Error
   return;
}

It's not a situation like if or for, it's just another {} As I left the block, I couldn't write d.

The professor said {} is a block in class Then does the stack go up and down for each block?

c memory stack

2022-09-22 22:18

1 Answers

As you asked, you can declare a variable on the top line of the brace, and if you leave the area, you can no longer use the identifier It may look similar to a stack to a programmer's

C's brace ({{}) is not a stack frame, but naming scope), so entering the brace does not push/pop into the stack.

The code you asked is theoretically an array d holds memory throughout the foo function.


2022-09-22 22:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.