C language program minor problem, I just learned C small white

  c++, question

Code 1: when a is a variable

#include<stdio.h>
 
 int * ccc();
 
 int main(void)
 
 {
 int* p = ccc();
 
 printf("%d\n",*p);
 printf("%d\n",*p);
 printf("%d\n",*p);
 printf("%d\n",*p);
 printf("%d\n",*p);
 return 0;
 }
 
 int * ccc()
 
 {
 int a = 5;
 
 return &a;
 
 }

Running results:
图片描述

Code 2: when variables are arrays

#include<stdio.h>
 
 int * ccc();
 
 int main(void)
 
 {
 int i;
 int* p = ccc();
 
 for(i = 0;  i <= 5;  i++)
 {
 printf("%d ",*(p + i));
 }
 return 0;
 }
 
 int * ccc()
 
 {
 int a[6] = {1,2,3,4,5,6};
 int * p = a;
 
 return p;
 
 }

Running results:
图片描述

Small White Question
1. Why is the operation result like this? Why can the local variable A be accessed normally for the first time when it is released, and why can it be accessed normally only for the first time? Aren’t all spaces released? The variable is an array. Why does the result look like this? It is strange that I want to know the specific principle.

Thank you all for being a great god.

It’s like this.
Local variables are stored in the stack area, and each time the stack is operated, only the pointer at the top of the stack is moved, and it will not take time and effort to reset the discarded stack memory, that is, the value still exists when the function returns. However, function jumps also use stack space, so some data will be changed when CPU makes function jumps. The reason why the first number will not change but will change later is probably because theprintfWhen the function jumps, it just uses the stack space after the first number.
If you define a particularly large array in your structure, and then type the information in the first structure in the same way, no accident will occur, and the second half of the array in the first structure will also change.