最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

Extern usage

工作和技术 crifan 1594浏览 0评论

Extern usage

By Crifan Li

Date: 18/06/2008

First, we should know the what is the meaning of the keyword: “extern” :

It means, this variable or function can be used in else-where !

Not means the variable or function is defined else-where.

So, “extern” should ONLY be seen in the original defined place, and its header file, NOT the place where we use the extern variable or function!!!!!

1.       in the C file, define the global variable ,

let’s say :

in item_audio_equalizer.c, we define a global variable:

const eq_struct eqList[EQ_MAX_NR] =

{

    0, "Normal",        -1, -1, -1, -1, -1,

    1, "Class",          -1, -1, -1, -5, -6,

    2, "Jazz",          6, -2, -3, -1, 4,

    3, "Pop",               -3, 4, 4, -2, -1,

    4, "Rock",             9, -3, -2, 7, 7,

    5, "Cust",              -1, -1, -1, -1, -1

};

This means, we defined a (global) variable here.

2.       in the C header file, exclaim it

let’s say:

in item_audio_equalizer.h, we exclaim it:

extern const eq_struct eqList[EQ_MAX_NR];

this means, this variable can be used in other place by other C file

just same as the functions exclaimed in this header file !

3.       in other C file, If you want to use that extern variable, you just need use it Directly !!!

do not need to add something like “extern const eq_struct eqList[EQ_MAX_NR];”

or “const eq_struct eqList[EQ_MAX_NR];”

Conclusion:

ONLY the functions or variables exclaimed in the header file of the original C file,

Can be used by other place!!!

The functions and variables, realized in C file but not exclaimed in header file, is means the private functions and variables ,only can be used by itself’s C file, can not be used in other place!

转载请注明:在路上 » Extern usage

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
79 queries in 0.143 seconds, using 22.11MB memory