码迷,mamicode.com
首页 > 其他好文 > 详细

static 修饰函数

时间:2018-04-04 23:19:56      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:局部变量   blog   技术分享   没有   bsp   变化   png   return   分享   

编程之路刚刚开始,错误难免,希望大家能够指出。

 

今天看static关键词的作用,发现都没有实际去解释static修饰的函数是如何的。

先上代码:

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <unistd.h>
 4 #include <fcntl.h>
 5 
 6 int fun1()
 7 {
 8     return 1;
 9 }
10 
11 static int fun2()
12 {
13     static int t1 = 2;
14     printf("t1 = %p\n",&t1);
15 
16     int t2 = 3;
17     printf("t2 = %p\n",&t2);
18 
19     return 2;
20 }
21 
22 int main(void)
23 {
24     int t3 = 0;
25     printf("t3 = %p\n",&t3);
26 
27     static int t4 = 0;
28     printf("t4 = %p\n",&t4);
29 
30     printf("fun1 = %p\n",&fun1);
31 
32     static int t6 = 2;
33     printf("t6 = %p\n",&t6);
34 
35     printf("fun2 = %p\n",&fun2);
36     fun2();
37 
38     int *t8 = (int *)malloc(5);
39     printf("t8 = %p\n",t8);
40 
41     return 0;
42 }

  

运行结果:

技术分享图片

 

得出的结论:

  static修饰的函数存放地址仍然在代码段,而该函数内的局部变量在栈,static修饰的变量也还在数据段。也就是说,static修饰的函数并没有在存放位置上发生变化。

 

static 修饰函数

标签:局部变量   blog   技术分享   没有   bsp   变化   png   return   分享   

原文地址:https://www.cnblogs.com/jiangyibo/p/8718968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!