Hi,
In the below code snippet can i replace char * to const char * and remove the strdup() function call and directly take the optarg value set by getopt()? I am advised to use const char * to skip the strdup function usage. Appreciate the help in advance.
/* Code Snippet */
char *dir = NULL;
char *bld = NULL;
int chr;
while ( ( chr = getopt( argc, argv, "d:a:b:f:" ) ) != -1 ) {
switch ( chr ) {
case 'd': //Directory parameter
dir = strdup( optarg );
if (dir == NULL) { /*Error*/ }
case 'b': //Build parameter
bld = strdup( optarg );
if (bld == NULL) { /*Error*/ }
...other code...
}
}
I really don't understand the need for doing so.
Edit: Thanks for the answers. It was really helpful. I modified the code to const char * and skipped the strdup call.
Thanks, Liju