当前位置: 代码迷 >> C语言 >> 关于static的应用
  详细解决方案

关于static的应用

热度:423   发布时间:2006-08-14 22:34:33.0
关于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  

----------------解决方案--------------------------------------------------------
bu ming bai ni shuo sha?

----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------

如果要了解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.


----------------解决方案--------------------------------------------------------
是外语外貌大学的就是不一样
----------------解决方案--------------------------------------------------------

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

第一个家static好像没什么用

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


----------------解决方案--------------------------------------------------------
以下是引用lj_860603在2006-8-14 23:40:28的发言:

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

谁说没有必要,对不会被修改的数据加上static能够增强数据的封装性,数据不易被破坏......
----------------解决方案--------------------------------------------------------
看上去第二个还是对的。
----------------解决方案--------------------------------------------------------

以下是引用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是静态局部变量,在整个函数中是不释放的。我是觉得如果没必要的话大可不用。节省内存空间。


----------------解决方案--------------------------------------------------------
第一个用不用static都不是差不多?没有实际意义吧!
----------------解决方案--------------------------------------------------------
  相关解决方案