extern和static指针用C [英] Extern and Static Pointers in C

查看:127
本文介绍了extern和static指针用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是静态和extern指针的使用?如果存在

Hi what could be the usage of static and extern pointer ?? if they exist

推荐答案

要回答你的问题什么时候可以使用它们,几个简单的例子:

To answer your question about when they could be used, a couple of simple examples:

一个静态指针可以被用来实现该总是返回的相同缓冲液的程序的功能,在第一次被调用分配它

A static pointer could be used to implement a function that always returns the same buffer to the program, allocating it the first time it is called:

char * GetBuffer() {
   static char * buff = 0;
   if ( buff == 0 ) {
       buff = malloc( BUFSIZE );
   }
   return buff;
}

(即,全局)指针可以被用于允许其它编译单元访问的主要参数一个外部

An extern (i.e. global) pointer could be used to allow other compilation units to access the parameters of main:

extern int ArgC = 0;
extern char ** ArgV = 0;

int main( int argc, char ** argv ) {
   ArgC = argc;
   ArgV = argv;
   ...
}

这篇关于extern和static指针用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆