I have the following condition in code:
Header file:
functionality.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
and functionality.c
#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.
}
The problem is if I initialize variable in header file then since the header file is included in multiple source file so the compiler gives multiple definition error.
If I only declare it in header file and initialize in one of the source file then the same variables are repeatedly written twice in code.
This make code look lengthy and awkward.
So is there a better way out like:
1. Either I declare and initialize at same place.
2. How can I initialize it through function and call it in main()
Or something else so that the code looks less.
Because we have to declaring huge set of variables in header and initializing them in source file with exact same signature. For our mentor it looks him like we are repeating the same thing. There should be a better way. Thanks....
max_adhaar_sizeexists somewhere. There's still only one definition and one variable in the generated program and in memory during run-time.