| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1069 人关注过本帖
标题:关于static的应用
只看楼主 加入收藏
ldliming
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2006-8-12
收藏
 问题点数:0 回复次数:9 
关于static的应用
请看下面的两个函数:
(1)
main()
{
static int a[3][4]={1,2,3,4,5,6,7,8,9,10,12};
int *arr[3]={a[0],a[1],a[2]};
int i,**p;
p=arr;
printf("p=%d,*p=%d,**p=%d\n",p,*p,**p);
}
(2)
main()
{
int a[3][4]={1,2,3,4,5,6,7,8,9,10,12};
int *arr[3]={a[0],a[1],a[2]};
int i,**p;
p=arr;
printf("p=%d,*p=%d,**p=%d\n",p,*p,**p);
}
为什么第一个函数运行正确的,而第二个函数却是错误的呢?在这里static起到什么作用呢?还望高手们详细解说static的用法。拜托,谢谢!!!!!!!!!!!
搜索更多相关主题的帖子: static 应用 函数 int arr 
2006-08-14 22:34
论坛
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1372
专家分:0
注 册:2006-3-27
收藏
得分:0 
bu ming bai ni shuo sha?

日出东方,唯我不败! 做任何东西都是耐得住寂寞,任何一个行业要有十年以上的积累才能成为专家
2006-08-14 22:54
lj_860603
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:714
专家分:0
注 册:2006-1-25
收藏
得分:0 

两个程序都没有问题。第一个程序加static倒没有必要。


我的原则很简单:不做不喜欢的事!
2006-08-14 23:40
lj_860603
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:714
专家分:0
注 册:2006-1-25
收藏
得分:0 

如果要了解static,看看《The C programming Language》的一段介绍吧:
================================
The variables sp and val in stack.c, and buf and bufp in getch.c, are for the private use of the functions in their respective source files, and are not meant to be accessed by anything else. The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled. External static thus provides a way to hide names like buf and bufp in the getch-ungetch combination, which must be external so they can be shared, yet which should not be visible to users of getch and ungetch.
Static storage is specified by prefixing the normal declaration with the word static. If the two routines and the two variables are compiled in one file, as in

static char buf[BUFSIZE]; /* buffer for ungetch */
static int bufp = 0; /* next free position in buf */

int getch(void) { ... }

void ungetch(int c) { ... }

then no other routine will be able to access buf and bufp, and those names will not conflict with the same names in other files of the same program. In the same way, the variables that push and pop use for stack manipulation can be hidden, by declaring sp and val to be static.
The external static declaration is most often used for variables, but it can be applied to functions as well. Normally, function names are global, visible to any part of the entire program. If a function is declared static, however, its name is invisible outside of the file in which it is declared.

The static declaration can also be applied to internal variables. Internal static variables are local to a particular function just as automatic variables are, but unlike automatics, they remain in existence rather than coming and going each time the function is activated. This means that internal static variables provide private, permanent storage within a single function.


我的原则很简单:不做不喜欢的事!
2006-08-14 23:41
触电
Rank: 1
等 级:新手上路
威 望:1
帖 子:228
专家分:0
注 册:2006-7-26
收藏
得分:0 
是外语外貌大学的就是不一样
2006-08-14 23:56
喝茶的小k
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2006-7-27
收藏
得分:0 

这两个程序的运行结果是一样的阿

第一个家static好像没什么用

那个int i;定义i有什么用阿


2006-08-15 00:19
多维数组
Rank: 1
等 级:新手上路
帖 子:238
专家分:0
注 册:2006-8-16
收藏
得分:0 
以下是引用lj_860603在2006-8-14 23:40:28的发言:

两个程序都没有问题。第一个程序加static倒没有必要。

谁说没有必要,对不会被修改的数据加上static能够增强数据的封装性,数据不易被破坏......


有事发邮件:tzp_1210@
2006-08-16 14:42
jiahu0215178
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2006-8-12
收藏
得分:0 
看上去第二个还是对的。
2006-08-16 19:44
wuyufenjr
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2006-7-20
收藏
得分:0 
以下是引用ldliming在2006-8-14 22:34:33的发言:
请看下面的两个函数:
(1)
main()
{
static int a[3][4]={1,2,3,4,5,6,7,8,9,10,12};
int *arr[3]={a[0],a[1],a[2]};
int i,**p;
p=arr;
printf("p=%d,*p=%d,**p=%d\n",p,*p,**p);
}
(2)
main()
{
int a[3][4]={1,2,3,4,5,6,7,8,9,10,12};
int *arr[3]={a[0],a[1],a[2]};
int i,**p;
p=arr;
printf("p=%d,*p=%d,**p=%d\n",p,*p,**p);
}
为什么第一个函数运行正确的,而第二个函数却是错误的呢?在这里static起到什么作用呢?还望高手们详细解说static的用法。拜托,谢谢!!!!!!!!!!!

static是静态局部变量,在整个函数中是不释放的。我是觉得如果没必要的话大可不用。节省内存空间。

2006-08-16 22:15
wuyufenjr
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2006-7-20
收藏
得分:0 
第一个用不用static都不是差不多?没有实际意义吧!
2006-08-16 22:16
快速回复:关于static的应用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015757 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved