The code below makes a copy of argc
and argv
arguments of main
into user defined structure. The copy is exactly the same as original arguments (argv
is terminated by NULL
as in main). This code does not handle any abnormal events (like malloc failures, signal interruption etc.).
You may want to avoid casting the result of malloc
(it is not required in C), there is a lot of debate whether this casting is a good or bad thing. The choice is yours. I value portability more, so I choose to cast, compile with all warnings enabled and eliminate all of them in the code (see the comments to this answer).
#include <string.h>
#include <stdlib.h>
struct my_struct
{
int argc;
char** argv;
};
int main(int argc,char** argv)
{
int i = 0;
size_t n = 0;
struct my_struct s;
memset(&s,0,sizeof(s));
s.argc = argc;
/* alloc one more element than 'argc' to terminate 's.argv' with NULL */
s.argv = (char**)malloc((argc + 1) * sizeof(char*));
/* terminate 's.argv' with NULL exactly as 'argv' in 'main' */
s.argv[argc] = NULL;
for(i = 0; i < argc; ++i)
{
n = strlen(argv[i]) + 1;
s.argv[i] = (char*)malloc(n);
strcpy(s.argv[i],argv[i]);
}
return 0;
}
Another option for copying the strings in argv
could be using strdup
function, then you could replace the three lines:
n = strlen(argv[i]) + 1;
s.argv[i] = (char*)malloc(n);
strcpy(s.argv[i],argv[i]);
with
s.argv[i] = strdup(argv[i]);