extern

How to define extern variable along with declaration?

…衆ロ難τιáo~ 提交于 2019-12-05 12:48:31
问题 Wiki says: The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a definition. It is done by assigning an initialization value to a variable . That means, an extern declaration that initializes the variable serves as a definition for that variable . So, /* Just for testing purpose only */ #include <stdio.h> extern int y

How to declare extern typedef struct?

瘦欲@ 提交于 2019-12-05 06:38:30
I have two c files, foo.c with the functionality and test_foo.c which test the functions of foo.c. Is there a way to access the struct typedef BAR I defined in foo.c in test_foo.c without using a header file? So far, I was able to avoid a h file so that the whole program would consist of foo.c. Thanks. foo.c typedef struct BAR_{...} bar; BAR *bar_new(...) {..} test_foo.c extern BAR *bar_new(...) error: expected declaration specifiers or ‘...’ before ‘BAR’ The answer is that there is one, and you should use an header file instead. You can copy the definition of the struct typedef struct BAR_{..

C: Is it legal to subscript an array of incomplete type?

你离开我真会死。 提交于 2019-12-05 06:02:28
I can't find the relevant bits in the standard, but gcc and clang allow it, so I guess I' wondering if it's a compiler extension or part of the language. Provide a link if you can. This can arise with things such as this: extern char arr[]; func(arr[7]); /*No error.*/ LATE EDIT: I figured I'd better get a clear understanding of this, which I never did although I had moved on, so starting a bounty which I will award to the first person to give me a clear, concise reference(es) in the C89 standard as to why this is allowed. C99 is acceptable if nobody can find the answer in C89, but you need to

C语言学习笔记--void

我是研究僧i 提交于 2019-12-05 05:10:45
void真正发挥的作用在于:   (1) 对函数返回的限定;   (2) 对函数参数的限定。 先给一个例子 定义函数 返回值 函数名(参数1,参数2,参数3,.......) {内容} int sum(int a,int b) { int c; return c;} 其中第一个int是返回值 就是别的函数调用此函数时这个函数给他的一个值。 如果调用时不需要返回值,则函数写为 void sum(int a,int b){....} 此时函数没有返回值 如果不需要参数 则int sum(void){...} 此时void的意义为空,就是没有参数的意思 如果都不要 则为void sum(void); ------------------------------------------------------------------------------------------------------------------------------------------------ *extern的用法 如果需要在一个源文件中引用另外一个源文件中定义的变量,我们只需在引用的文件中将变量加上 extern 关键字的声明即可。 #include < stdio.h > /* 外部变量声明 */ extern int x ; extern int y ; int addtwonum ( )

Externing functions in C++

南楼画角 提交于 2019-12-05 04:55:57
When externing a function in the cpp file does the compiler treat these differently? extern void foo(char * dataPtr); void foo(char *); extern void foo(char * ); I am wondering because I have see all these in code and not sure what the difference is. Case by case: extern void foo(char * dataPtr); functions have external linkage by default, so the extern is not necessary - this is equivalent to: void foo(char * dataPtr); Parameter names are not significant in function declarations, so the above is equivalent to: void foo(char * ); Use whichever you feel happiest with. No. They all are same

文件读写路径问题

瘦欲@ 提交于 2019-12-05 03:07:50
extern CWinApp theApp; char szAppName[MAX_PATH]; :: GetModuleFileName(theApp.m_hinstance, szAppName, MAX_PATH); CString strAppFullName; strAppFullName.Format(“%s”, szAppName); CString strTempPath = _T(“”); strTempPath = strAppFullName.Left(strAppFullName.ReverseFind(‘//’)+1)+_T(“Temp”); ------------------------------------------------------------------------------------ http://www.xuebuyuan.com/1791829.html 来源: https://www.cnblogs.com/music-liang/p/11901059.html

How to declare extern 2d-array in header?

江枫思渺然 提交于 2019-12-05 02:39:31
We have this declaration in LCD.c: unsigned char LCD[8][64] = {((unsigned char) 0)}; And in LCD.h we want to have something like: extern unsigned char LCD[][]; We get this error: Error[Pe098]: an array may not have elements of this type You need, at a minimum, to include the right-most column size for a 2-D array. You can declare it like this: extern unsigned char LCD[][64]; Otherwise the compiler would not be able to compute the offset after the first row. In C an array does not contain information about the size of each one of its dimensions. Therefore, the compiler needs to know how large

Why can extern be applied to definitions?

你说的曾经没有我的故事 提交于 2019-12-05 02:33:54
Why is this legal? extern int foo = 0xF00; // Gets a warning, still compiles extern void bar() { // No warning int x; } Is there a reason to why this is allowed? Johannes Schaub - litb Sometimes it's useful extern const int foo = 0xF00; Without the extern , in C++ foo would be static and have internal linkage (which means you could not use foo from another translation unit). The extern in both cases in your example is redundant. In C99 an extern can make a difference for inline functions. . In the function case, I think it is just like writing: extern void bar(); void bar() { int x; } which

C Primer Plus 第12章 12.2 存储类说明符

可紊 提交于 2019-12-04 23:41:18
C语言中有5个关于存储类说明符的关键字,它们是auto、register、static、extern以及typedef。关键字typedef与内存存储无关,由于语法原因被归入此类。特别地,不可以在一个声明中使用一个以上存储类说明符,这意味着不能将其他任一存储类说明符作为typedef的一部分。 说明符auto表明一个变量具有自动存储时期。该说明符只能用在具有代码块作用域的变量声明中,而这样的变量已经拥有自动存储时期,因此它主要用来明确指出意图,使程序更易读。 说明符register也只能用于具有代码块作用域的变量。它将一个变量归入寄存器存储类,这相当于请求将该 变量存储在一个寄存器内,以更快地存取。它的使用也使您不能获得变量的地址。 说明符static在用于具有代码块作用域的变量的声明时,使该变量具有静态存储时期,从而得以在程序运行期间(即使在包含该变量的代码块并没有运行时)存在并保留其值。变量仍具有代码块作用域和空链接。static在用于具有文件作用域的变量的声明时,表明该变量具有内部链接。 说明符extern表明您在声明一个已经在别处定义了的变量。如果包含extern的声明具有文件作用域,所指向的变量必然具有外部链接。如果包含extern的声明具有代码块作用域,所指向的变量可能具有外部链接也可能具有内部链接,这取决于该变量的定义声明。 下面给出了一个使用全部5种存储类的小程序

What is the Effect of Declaring 'extern “C”' in the Header to a C++ Shared Library?

这一生的挚爱 提交于 2019-12-04 23:40:07
问题 Based on this question I understand the purpose of the construct in linking C libraries with C++ code. Now suppose the following: I have a '.so' shared library compiled with a C++ compiler. The header has a 'typedef stuct' and a number of function declarations. If the header includes the extern "C" declaration... #ifdef __cplusplus extern "C" { #endif // typedef struct ...; // function decls #ifdef __cplusplus } #endif ... what is the effect? Specifically I'm wondering if there are any