How to read "string" from a command line argument in C?

I have a question about passing parameters through the command line.

My main() looks like

 int main(int argc, char **argv){ int b, d, n, flag; char *init_d, tst_dir[100]; argv++; init_d=*(argv++); //printf(); <--------What do I have to do to init_d so that I can print it later? 

If argv is a pointer to an array of pointers, I assign init_d to point to the value pointed to by the argv pointer points to? (If that even makes sense)

I assume that I need to get this value in an array of characters in order to print it, but if I do not know the size of the "string" that I pass, I'm not sure how to achieve this. For example, if I run my code './myprogram hello' compared to './myprogram alongerinput'

+7
source share
6 answers

You can print arguments without transferring them to character arrays. They are zero-terminated C lines and printf eats them for breakfast:

 for (i=0; i<argc; i++) printf("%s\n", argv[i]); 
+12
source

Here is an example that converts command line arguments to a single line:

 #include <stdlib.h> #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { if (argc < 1) return 0; int i; int strsize = 0; for (i=1; i<argc; i++) { strsize += strlen(argv[i]); if (argc > i+1) strsize++; } printf("strsize: %d\n", strsize); char *cmdstring; cmdstring = malloc(strsize); cmdstring[0] = '\0'; for (i=1; i<argc; i++) { strcat(cmdstring, argv[i]); if (argc > i+1) strcat(cmdstring, " "); } printf("cmdstring: %s\n", cmdstring); return 0; } 
+3
source
 int main(int argc, char **argv){ while(--argc>0) printf("%s\n",*++argv); return 0; } argc: argument count ie number of arguments in command line argv: argument vector 

if your program is myProg , then myProg hello on the command line has argc = 2 (including the name of the program), argv [0] is "myProg", and argv [1] is "hello"

+2
source

After launch

 init_d = *(argv++); 

(parens not needed, btw), init_d is a pointer to a character. In C, a string is a character pointer that adheres to the contract that if it is advanced far enough, a null character ('\ 0') will eventually be reached, signifying the end of the string. In other words, init_d is now exactly the line you want. You can print it using

 printf("%s", init_d); 

Note. By the way, btw, that * argv ++ gives you the first argv element, which is actually the name of the function on the command line. You probably need the first command line argument you get with * ++ argv.

+2
source

argv is a pointer to an array of strings with a null character. You do not need to know the size of the string; all you have to do is point to the value, since the end of the line is indicated by the character "\ 0".

 char* init_d = argv[0]; printf("%s", init_d); 

If you want to know the length of a string, you can use strlen(argv[0]) .

+1
source

You can print a line indicating with init_d with

 printf("%s", init_d); 

"% s" prints all characters in the string until it reaches "\ 0". In C, strings have zero termination.

0
source

All Articles