Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 头文件中全局变量的初始化_C_Linux_Gcc - Fatal编程技术网

C 头文件中全局变量的初始化

C 头文件中全局变量的初始化,c,linux,gcc,C,Linux,Gcc,我在代码中有以下条件: 头文件: 功能。h: #ifndef functionality_h #define functionality_h #include <stdio.h> #include <stdlib.h> #include <string.h> /*The field value attribute declaration*/ #define size_adhaar 3000 #define size_aspid 8 #define

我在代码中有以下条件:

头文件:

功能。h:

#ifndef functionality_h
#define functionality_h


#include  <stdio.h>
#include  <stdlib.h>
#include  <string.h>


/*The field value attribute declaration*/
#define size_adhaar 3000
#define size_aspid 8
#define size_input 65
#define size_esignClass 2
#define size_sc 2
#define size_ver 4
#define size_txn 45


/*Replacement string declaration */

extern const char replace_adhaar[];
extern const char replace_aspid[];
extern const char replace_input[];
extern const char replace_esignClass[];
extern const char replace_sc[];
extern const char replace_ver[];
extern const char replace_txn[];

/*Max length of field attribute*/

extern int max_adhaar_size;
extern int max_aspid_size;
extern int max_input_size;
extern int max_esignClass_size;
extern int max_sc_size;
extern int max_ver_size;
extern int max_txn_size;



/*
 The declaration of the field attribute.
*/


char adhaar[size_adhaar];
char aspid[size_aspid];
char input[size_input];
char esignClass[size_esignClass];
char sc[size_sc];
char ver [size_ver];
char txn[size_txn];

void incorrect_val_test(FILE *, FILE *, FILE *, FILE *, FILE *, FILE *, FILE *, FILE *, FILE *);

#endif
问题是如果我在头文件中初始化变量,那么因为头文件包含在多个源文件中,所以编译器会给出多个定义错误

如果我只在头文件中声明它,并在一个源文件中初始化它,那么相同的变量会在代码中重复写入两次

这使得代码看起来冗长而笨拙

那么有没有更好的出路,比如:

1。要么在同一位置声明和初始化。
2.如何通过函数初始化它并在main()中调用它
或其他使代码看起来更少的内容。


因为我们必须在头文件中声明大量变量,并在源文件中用完全相同的签名初始化它们。对我们的导师来说,我们似乎在重复同样的事情。应该有更好的办法。谢谢

您正在声明一组庞大的变量-这绝对不是干净的代码(Bob叔叔的干净代码可以帮助您)

若你们真的想这么做,你们需要在头文件中声明所有带有“extern”的全局变量,并在源文件中定义它们。它使您的所有文件(包括带有全局变量的头文件)都“知道”您的变量,并使您的所有变量在内存中只创建一次(因为每个源文件只编译一次)


请记住,在99%的情况下,您可以编写没有任何全局变量的程序。

没错,但对于所有源文件,该定义仍然是一次,不是吗?声明只是编译器的信息,因此我们知道,例如variable
max_adhaar\u size
存在于某个地方。在运行时,生成的程序和内存中仍然只有一个定义和一个变量。此外,如果您在全局声明“大量变量集”,那么您很可能是做错了。好吧!!!!!先生,我怎样才能在一个地方声明一组局部变量,并在源文件中使用它,就像我想在头文件中声明和定义一个地方的所有变量一样。我不能在本地执行函数,因为该函数被客户端程序反复调用。我希望你能理解我的困境,问题是我希望所有变量都在一个地方,同时也在函数的局部和外部。我必须在函数中使用。谢谢,伙计,但我需要全局变量的那种要求,并且像init函数一样一次性初始化。如果有出路,请帮助。。。。。
#include "functionality.h"
#include "function.h"

/* Initialization Block starts*/

const char replace_adhaar[]     = "REPLACE_Adhaarii_";
const char replace_aspid[]      = "REPLACE_aspidii_";
const char replace_input[]      = "REPLACE_Inputii_";
const char replace_esignClass[] = "REPLACE_esignClassii_";
const char replace_sc[]         = "REPLACE_scii_";
const char replace_ver[]        = "REPLACE_verii_";
const char replace_txn[]        = "REPLACE_txnii_";

/*Max length of field attribute*/

int max_adhaar_size      = 3000;
int max_aspid_size       = size_aspid+sizeof(replace_aspid);
int max_input_size       = size_input+sizeof(replace_input);
int max_esignClass_size  = size_esignClass+sizeof(replace_esignClass);
int max_sc_size          = size_sc+sizeof(replace_sc);
int max_ver_size         = size_ver+sizeof(replace_ver);
int max_txn_size         = size_txn+sizeof(replace_txn);


/*Field true value initialization*/

char adhaar[size_adhaar] = {"RbEY0VVRJNVFtNDhMMGh0WVdNK1BDOUJkWFJvPQ=="};
char aspid[]             = {"ASP-101"};
char input[]             = {"936a185caaa266bb9cbe981e9e05c"};
char esignClass[]        = {"1"};
char sc[]                = {"Y"};
char ver[]               = {"1.0"};
char txn[]               = {"2250dc54-79e8-42ad-b86b-7ec09759"};

/*Initialization Block end*/

void incorrect_val_test(FILE *fp_adhaar, FILE *fp_aspid, FILE *fp_input, FILE *fp_esignClass, FILE *fp_sc, FILE *fp_ver, FILE *fp_ts, FILE *fp_txn, FILE *fp_vtxn)
{
     //Some processing done here.
}