1

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....

5
  • 1
    Right, but still, for all of the source files, that definitions is still once, isn't it? Commented May 4, 2015 at 7:27
  • A declaration is only information for the compiler, so it's know that e.g. variable max_adhaar_size exists somewhere. There's still only one definition and one variable in the generated program and in memory during run-time. Commented May 4, 2015 at 7:29
  • 3
    Also, if you are declaring a "huge set of variables" globally, then you're most likely doing it wrong. Commented May 4, 2015 at 7:33
  • Ok !!!!! sir how can I declare a set of local variables at one place an use it in a source file like I want to declare and define all variable at one place as in header file. I can not do in function locally because the function is called repeatedly by client program. I hope you understand the dilema of mine. Commented May 4, 2015 at 19:02
  • The problem is I want all the variable at one place and local as well and out side the function. And I have to use in a function. Commented May 4, 2015 at 19:03

1 Answer 1

1

You are declaring huge set of variables - it's definitely not clean code (Uncle Bob's Clean Code can help you)

If you really want to do it - you need to declare all global variables with "extern" in header file and define them in source file. It causes all your files including header with globals "know" about your variables and it makes all your variables be created only once in memory (as every source file is compiled exactly once).

Remember - in 99% of cases you can write your program without any global variables.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks buddy but I need that kind of requirement for global variables and to initialize in a single go like init functions do. If there is a way out please help.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.