Website tutorial; Command line arguments

This is a discussion on Website tutorial; Command line arguments within the C++ Programming forums, part of the General Programming Boards category; SO yeah I'm still working on the tutorial, still happy with it in general Now I came to lesson 14 ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    9

    Website tutorial; Command line arguments

    SO yeah I'm still working on the tutorial, still happy with it in general Now I came to lesson 14 about command line arguments. It's all pretty simple, and I didn't have much of any problem going through the lesson, and writing a small application derived of the code example given.

    However hwn I quized myself I messed up on one of the questions.

    Code:
    ...
    
    int main ( int argc, char *argv[] )
    
    ...
    3. What type is argv?
    A. char *
    B. int
    C. char **
    D. It's not a variable
    Now the quiz tells me that the type of argv is "char**", could someone explain to me the difference between "char *" and "char **", and if not entirely obvious, why "char **" is the correct anwser.

    Very much appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    A char* is a pointer to a char. A char** is a pointer to a pointer to a char.

    Now, arrays decay to a pointer to their first element when passed as arguments to functions. As such, the "char* argv[]" parameter of main() actually ends up as a char**, though we think of it as an array of null-terminated strings of characters.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    char* argv[] is the same as char** argv.

    char* argv[] indicates it's an array of pointer to char. Strings are pretty much a null terminated set of sequential characters, with char* indicating a pointer to the address of the first character. Pointers in themselves can be used to reference arrays using pointer arithmetic.

    char** argv indicates it's a pointer to char*'s. So it's the first address of an array of pointers to characters.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    9
    I think I'm starting to understand it (though it's still quite confusing in my mind).

    char* indicates a pointer to a something.
    argv[] indicates an array

    So, the type of argv[] would be char*, however the type of argv's first element would be char**.

    If I'm wrong to this point, it more or less makes my next question stupid, if I'm right to this point then...

    I'm still a bit confused about how "argv" evaluates to the first item within the argv[] array.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    So, the type of argv[] would be char*, however the type of argv's first element would be char**.
    I think you got it the other way round. By virtue of being an array of strings, argv is a char**. The first element of argv is a string, and thus is a char*.

    I'm still a bit confused about how "argv" evaluates to the first item within the argv[] array.
    Consider this example program:
    Code:
    #include <iostream>
    
    void tellSize(int nums[])
    {
        std::cout << sizeof(nums) << std::endl;
    }
    
    int main()
    {
        int numbers[20];
        std::cout << sizeof(numbers) << std::endl;
        tellSize(numbers);
    }
    Assuming that you are working with 4 byte ints (and pointers), you will find that the output of that program is:
    80
    4

    In the main() function, numbers is an array of 20 ints, and thus its size in bytes in 20 * 4 = 80. When it is passed to tellSize(), the nums is actually a pointer to the first element of numbers. Since it is a pointer, its size is 4 bytes, not 80 bytes, although the array it points to is still 80 bytes in size.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    to illustrate the address location
    Code:
    #include <iostream>
    
    using namespace std;
    
    void printFirst(int* first)
    {
        cout << *first << endl;
    }
    
    int main()
    {
    	int arr[4] = {1, 2, 3, 4};
    	printFirst(arr);
    	return 0;
    }
    printFirst takes a pointer to int. In main I create an array with 4 elements. Then when I call printFirst I pass it the variable name, which is actually the address of the first element in the array. I would get the same effect as calling printFirst(&arr[0]);

    if I wanted to iterate through the array I owuld just increment the pointer in the printFirst Method as so

    Code:
    void printFirst(int* first)
    {
        for(int i = 0; i < 4; ++i)
        {
            cout << *first << " ";
            ++first;
        }
        cout << endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  2. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 01:11 PM
  5. My DirectInput tutorial....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 11:32 PM

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