Some questions regarding argc and argv in C

This is a discussion on Some questions regarding argc and argv in C within the C Programming forums, part of the General Programming Boards category; Consider the following code: Code: #include <stdio.h> int main (int argc, char* argv[]) { int i; for (i = 0; ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    15

    Some questions regarding argc and argv in C

    Consider the following code:
    Code:
    #include <stdio.h>
     
    int main (int argc, char* argv[])
    {
        int i;
        for (i = 0; i < argc; i++)   
        {
         printf("%s %s\n", argv[i], argv[i]+i);  
        }
      return 0;
    }
    Being new to programming, I am having a tough time in developing the right thought process in anayzing and writing functions with argument count and argument vector.

    First of all, what are they used for? Is there any advantage in using argc and argv instead of the more commonly seen ones or are they used for specific purposes?

    Second of all, the code above makes little or no sense to me. Is there a way to describe the loop above using a psuedocode? If so, please let me know the basic idea of the program above.

    Lastly, why is there a star infront of the argv[]....that would make argv a double pointer wouldn't it? then argv[i] + i would be a pointer to an integer...I'm confused help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    argc and argv are the more commonly seen ones, probably on a ratio exceeding a thousand to one.

    argv is an array ([]) of pointers-to-character (char *). You probably want to think of pointers-to-character as "strings", which is fine since the operating system guarantees you that the command-line arguments it passes to you are bona fide strings. So argv is an array of strings.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by Hybodus View Post
    Second of all, the code above makes little or no sense to me. Is there a way to describe the loop above using a psuedocode? If so, please let me know the basic idea of the program above.
    The code is a pretty bad example.

    It is printing every command line argument, and then the same argument starting an an offset. (argv[i] + i) means a string starting at the i'th character of argv[i]. So if i is 2 and argv[2] is "hello", the output from printing argv[i] and argv[i] + i is "hello llo".

    That code gives undefined behaviour if the length of any of the argv[i]'s is less than i (for example, if argv[7] is "hello", argv[7] + 7 points past the end of the string).


    Quote Originally Posted by Hybodus View Post
    Lastly, why is there a star infront of the argv[]....that would make argv a double pointer wouldn't it?
    There are some circumstances where passing a pointer and an array are equivalent, so the syntaxes are interchangeable.

    This is one of them. However, keep in mind that a pointer and and array are different things, and the ability to use them interchangeably does break down with multidimensional arrays versus pointers to pointers.

    Quote Originally Posted by Hybodus View Post
    then argv[i] + i would be a pointer to an integer...I'm confused help!
    The result of "pointer_to_char + int" is a pointer_to_char, not an int, and not a pointer to an int.

    argv[i] is a pointer to char (which points at the first character [index zero] in a string). argv[i] + i is also a pointer to char, except that it points at the character in the string with index i (if no such character exists, accessing that character - called dereferencing the pointer - gives undefined behaviour, as I mentioned above)
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argc, argv
    By Martas in forum C Programming
    Replies: 6
    Last Post: 11-19-2009, 08:39 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 05:09 AM
  3. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  4. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  5. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21