23

So I just learnt that we can give two arguments into main function namely "argc" and "argv. However I am not able to understand what argv is in this: int main(int argc, char* argv[]);

Is argv an array of characters? or is it an array of pointers pointing to characters? Either way I am looking for a way to print out the arguments that the user passes to this program. This is the code the I wrote, but it's not printing the argv's so to speak. What's wrong in it? I guess it's my understanding of argv that's making this code incorrect.

#include<stdio.h>
int main(int argc, char *argv[])
{
    int i;
    printf("%d\n",argc);
    for(i=0;i<argc-1;i++)
    {
        printf("%s",*argv[i]);
    }
    return 0;
}

After the suggestions that I got from answers, I corrected my code as follows.

#include<stdio.h>
int main(int argc, char *argv[])
{
    int i;
    printf("%d\n",argc);
    for(i=1;i<argc;i++)
    {
        printf("%s",argv[i]);
    }
    return 0;
}

And I am using Ubuntu Linux in VMWare on my windows 8.1 pc. This is the output that I am getting. It's just printing argc and after that nothing. What's the problem? Is it the way I am compiling it or something in Linux terminal?

The snapshot of my terminal

In the above figure, I want the numbers 2,4,5,3 to be printed again, but they are not getting printed.

Thanks.

5
  • 2
    Enable warning in your compiler and pay attention to what it complains about. Mar 8, 2016 at 6:50
  • This is interesting; someone is upvoting all answers together...
    – 3442
    Mar 8, 2016 at 8:23
  • I was the one who upvoted all the answers because all of them were right. However my problem is not yet resolved, so I did not choose the right answer yet. and I have no idea who downvoted the answers in the first place. Thanks.
    – user5987642
    Mar 8, 2016 at 8:25
  • @everyone Sorry I am new here. So if I edit the question, I guess others who have already answered won't get notified. I read online that I should simply comment here so that others can notice that I have edited my question. So I wanted all of you to know that I have edited my question and I want answer for that. Thanks a lot for your help.
    – user5987642
    Mar 8, 2016 at 8:28
  • 3
    @rohitkrishna094 You are right with your observation, that others are not automatically notified about your edit. Leaving a comment like "Edited my question" does not notify anyone but it helps those who re-visit to immediately notice your update. With so many answers that's enough. If you ever happen to get only a single answer, your best bet is to comment the answer instead of your question - this really notifies the author of the answer.
    – grek40
    Mar 8, 2016 at 10:29

7 Answers 7

29

Let's explain things step by step. First of all, when you invoke your program by calling something like...

./my-program arg0 arg1 arg2

You're passing it a series of three arguments, right? And each argument is a string, right? Now, the main function, can have one of two prototypes, as specified by the C standard...

int main(); // Let's not worry about this for now
int main(int argc, char **argv);

The idea is that main is able to handle the arguments that you provided. argc provides the number of arguments. If you noticed, while passing three arguments, argc is 4! This happens because a first argument is passed before all others, ./my-program, and lets your program recognize itself.

Now, what does char **argv mean? Something of the form X* is a pointer to X, right? So, char * is a pointer to char, and char ** is a pointer to pointer to char. In C, a string is simply a zero-terminated array of char, and an array can be "degraded" into a pointer. This means that argv is an array of strings, the first of which, argv[0], is the program's name.

Now, the C standard allows you to write any "compatible" prototype for main. For instance, you can write any of these...

int main(int argc, const char *const argv[]);
int main(int argc, const char *argv[])
int main(int argc, const char **argv);
int main(int argc, const char *const *const argv);

You don't need to understand what they all mean now, just that argv is an array of strings, and that you should never modify strings as the original main prototype appears to trust you. Now, knowing that arguments start at argv[1], your code...

for(int i=0;i<argc-1;i++)

Means: "For each i in the range of 0 through argc - 1".

    printf("%s",*argv[i]);

Means: "Print the first character of the ith element of argv". Why would this be wrong? First of all, you're printing a char, and telling printf that it is a string. This has undefined behavior. Then, you're iterating over the first ith elements of argv. This means that the first, "non-argument" element will be included in the mix, and the last argument won't be. To solve it, write something like...

for(int i = 1; i < argc; i++)

Means: "For each i in the range from 1 up to argc".

    printf("%s", argv[i]);

Means: "Print the ith element of argv to stdout.

2
  • 2
    Sry, but this "This means that argv is an array of strings" (at least taken out of the somewhat misleading context) simply is wrong. argv is a pointer to the 1st element of array of pointers to char-pointers. With the later referencing char-arrays, C-"strings". An "array of strings" definitely is something different (char[47][11] for example).
    – alk
    Mar 13, 2016 at 12:08
  • Also the const in you example signatures for main() is wrong, at least for C.
    – alk
    Mar 13, 2016 at 12:30
7

Is argv an array of characters? or is it an array of pointers pointing to characters?

argv is a pointer to pointer to char. When a list of arguments are passed through command line then an array of char pointers are created and each of these pointers points to each of these arguments, stored in the form of strings, along with the program name. argv points to the first pointer of this char * array. Therefore, argv[i] is a pointer to char.

                           +--------------------------+
              +----+  +--> | argument 1 (program name)|
argv[0]+----> |    |  |    +--------------------------+
              |  +----+
              +----|       +--------------------------+
              |  +-------> |       argument 2         |
              |    |       +--------------------------+
              +----+
              |  +----+    +--------------------------+
              |    |  +--> |       argument 3         |
              +----+       +--------------------------+
           "char *" array

You need to change

printf("%s",*argv[i]);  

to

printf("%s",argv[i]);  

*argv[i] is of type char. %s expects a type of char *.

0
5

argv is a NULL terminated array of strings. So one doesn't even need argc to print all the arguments passed to the main function. So if argv points to NULL then that's the end of the arguments passed.

So to sum it up, you can do this with a simple while loop, using only argv:

    while (*argv != NULL)
    {
            printf("%s\n", *argv);
            argv++;
    }
4

char *argv[] is pointer to pointer to char because arrays in function arguments are automatically converted to pointer pointing at elements of the array.

You invoked undefined behavior by passing data having wrong type to printf(): %s expects char*, but you passed char (converted to int for variable number arguments).

Remove the extra * o dereference the pointer.

#include<stdio.h>
int main(int argc, char *argv[])
{
    int i;
    printf("%d\n",argc);
    for(i=0;i<argc-1;i++)
    {
        printf("%s",argv[i]);
    }
    return 0;
}

Also you may want i<argc instead of i<argc-1. Why not print the last argument?

0
3

In your edited question you have fixed your main problem. Now notice that your prompt looks like

./mygrep245YourName@ubuntu:~/test$

Here ./mygrep245 is your output after you printed "5\n". You forgot to print line feeds, thus all the output and the subsequent shell output has been concatenated as it is. And the last argument 3 wasn't printed because you put i<argc-1 instead of i<argc or i<=argc-1 in your loop condition.

1

You can refer the document:

As you can see, main now has arguments. The name of the variable argc stands for "argument count"; argc contains the number of arguments passed to the program. The name of the variable argv stands for "argument vector". A vector is a one-dimensional array, and argv is a one-dimensional array of strings. Each string is one of the arguments that was passed to the program.

If you need to print it simply remove the * and try this:

#include<stdio.h>
int main(int argc, char *argv[])
{
    int i;
    printf("%d\n",argc);
    for(i=0;i<argc-1;i++)
    {
        printf("%s",argv[i]);   //remove * from here
    }
    return 0;
}
-1

Regarding

Is argv an array of characters? or is it an array of pointers pointing to characters?

argv is an array of c-strings (where each c-string is a pointer to characters). Consider the following command line:

$ your-executable arg1 "arg2 with space"

It would result in an array of strings with the following content (pseudo code)

argv[0] EQUALS "your-executable"
argv[1] EQUALS "arg1"
argv[2] EQUALS "arg2 with space"

As others mentioned, remove the * from printf("%s", *argv[i]); and fix your loop condition if you want to print all arguments

6
  • 2
    It'ld be better not to teach the OP to do things like if(argv[0] == "your-executable") in real code...
    – 3442
    Mar 8, 2016 at 7:11
  • BTW: I'm not the downvoter, if someone happens to suspect due to timings.
    – 3442
    Mar 8, 2016 at 7:13
  • @KemyLand I decided against editing, since I already included a note about usability of this pseudo-code in C. However, I can understand that some do not like to see code that needs to be adapted in order to work properly.
    – grek40
    Mar 8, 2016 at 10:22
  • Such pseudo-code is really misleading, especially because it does compile as C code and will silently always say "no" (or sometimes "yes" in very unusual circumstances). I have to downvote to move the answer away from views (until it's changed, of course).
    – Ruslan
    Mar 8, 2016 at 15:06
  • @Ruslan well if you absolutely insist - made it more obvious pseudo code.
    – grek40
    Mar 8, 2016 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.