Friday, 27 September 2013

Copy command line arg to a variable C

Copy command line arg to a variable C

How can I copy a command line arg to a variable in c? If was to do the
following.
myprog "Hello, world!"
I want to store the value of the parameter in a char variable. Not sure if
I am going in the right direction here.
Assuming only 1 parameter will be passed in always.
int main (int argc, char *argv[]){
int len;
len=strlen(argv[1]);
char *array;
array= malloc((len +1) * sizeof(char));
int i;
for(i=0;i<=len;i++){
// This does not work... am a little lost
array[i]=*(argv+1)[i];
}
...
}
Can someone point me in the right direction.

No comments:

Post a Comment