initializing char pointers
Yes, it's good idea. Google Code Style recommends:
- To initialize all your variables even if you don't need them right now.
Initialize pointers by
NULL
,int
's by 0 andfloat
's by 0.0 -- just for better readability.int i = 0; double x = 0.0; char* c = NULL;
It is good practice to initialize all variables.
You cannot store a string in a pointer.
Your definition of mgt_dev_name
is good, but you need to point it somewhere with space for your string. Either malloc()
that space or use a previously defined array of characters.
char *mgt_dev_name = NULL;
char data[4200];
/* ... */
mgt_dev_name = data; /* use array */
/* ... */
mgt_dev_name = malloc(4200);
if (mgt_dev_name != NULL) {
/* use malloc'd space */
free(mgt_dev_name);
} else {
/* error: not enough memory */
}
If you're asking whether it's necessary, or whether it's a good idea to initialize the variable to NULL
before you set it to something else later on: It's not necessary to initialize it to NULL
, it won't make any difference for the functionality of your program.
Note that in programming, it's important to understand every line of code - why it's there and what exactly it's doing. Don't do things without knowing what they mean or without understanding why you're doing them.