16

I've a question about passing in parameters via 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'm assigning init_d to point to the value being pointed to by the pointer argv points to? (If that even makes sense)

I assume I have to get that value into a character array in order to print it out but if I don't know the size of the "string" I am passing in, I am not sure how to achieve this. For instance if i run my code './myprogram hello' compared to './myprogram alongerinput'

Piper
  • 1,266
  • 3
  • 15
  • 26
mike
  • 3,339
  • 6
  • 30
  • 34
  • 1
    Okay thanks everybody I apologize for my stupidity I kept getting segmentation faults but I see why now. – mike Feb 18 '11 at 20:21

6 Answers6

21

You can print the arguments without transferring them into character arrays. They are null-terminated C strings and printf eats them for breakfast:

for (i=0; i<argc; i++)
  printf("%s\n", argv[i]);  
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Well I don't want to print the arguments right away, I need to store the value passed in a variable which I want to print later in a function – mike Feb 18 '11 at 20:17
  • 1
    @mike OK, save them away. The OS allocates the space for `argv` and since your entire program is contained inside `main` and whatever it calls, the memory will be valid for the duration. – David Heffernan Feb 18 '11 at 20:18
  • usually we'd want to print from `argv[1]` onwards as `argv[0]` is the name of the program if I read the Kerrigan book correctly just now. – mLstudent33 Dec 20 '19 at 00:48
5

Here is example that converts command line arguments to single string:

#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;
}
Ari Malinen
  • 576
  • 2
  • 6
  • 20
4

argv is a pointer to an array of null-terminated strings. You do not need to know the size of the string; all you need to do is point to the value since the end of the string is indicated with a '\0'.

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

If you did want to know the length of the string you could use strlen(argv[0]).

Marlon
  • 19,924
  • 12
  • 70
  • 101
3
int main(int argc, char **argv){ 

    while(--argc>0)
       printf("%s\n",*++argv);
    return 0;
}

argc: argument count i.e. number of arguments in command line
argv: argument vector 

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

Cratylus
  • 52,998
  • 69
  • 209
  • 339
2

After running

init_d = *(argv++);

(parens not necessary, 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, eventually a null character ('\0') will be reached, signifying the end of the string. In other words, init_d is now precisely the string you want. You can print it with

printf("%s", init_d);

Note, btw, that *argv++ is giving you the first element of argv, which is actually the name of the function on the command line. You probably want the first command line argument, which you'll get with *++argv.

charleyc
  • 1,699
  • 12
  • 10
0

You can output the string you pointing with init_d at with

printf("%s", init_d);

"%s" output all characters of the string until it reach a '\0'. In C, strings are null terminated.

Fox32
  • 13,126
  • 9
  • 50
  • 71