Thanks again....

This is a discussion on Thanks again.... within the C++ Programming forums, part of the General Programming Boards category; Well if we look at the one way to write it, char **argv, remember that an array decays to a ...

  1. #31
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,750
    Well if we look at the one way to write it, char **argv, remember that an array decays to a pointer to the first element. This means that looking from right to left, argv is the identifier, and the * is the first dimension of the array, followed by the type of the elements, char *. char *argv[] communicates the same information in a different way: the brackets mean that we have a one dimensional array of variable length, and char * is the type of the elements.

  2. #32
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by whiteflags View Post
    Well if we look at the one way to write it, char **argv, remember that an array decays to a pointer to the first element. This means that looking from right to left, argv is the identifier, and the * is the first dimension of the array, followed by the type of the elements, char *. char *argv[] communicates the same information in a different way: the brackets mean that we have a one dimensional array of variable length, and char * is the type of the elements.
    hi whiteflags,
    your explanation makes it almost clearer.
    One thing char* *argv,
    *argv must point to a valid one dimensional array then?

    By the way, I wouldn't have know how to separate it that way, I thought it was just char** as a whole.
    You ended that sentence with a preposition...Bastard!

  3. #33
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,750
    >>*argv must point to a valid one dimensional array then?
    You're taking my words in a different, inapplicable context. I was merely trying to explain the semantics of the phrase "char **argv", where it is a parameter in main()'s parameter list, because that is what you asked. Read my post again.

    If you are going to use argv, then you have to understand the resulting type of an expression; think about what the operators will do. The expression argv is a one-dimensional array, while the expression *argv is the first element in the array of type char *.

    >>By the way, I wouldn't have know how to separate it that way, I thought it was just char** as a whole.
    Being able to understand type declarations is an important part of C literacy.

  4. #34
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by whiteflags View Post
    You're taking my words in a different, inapplicable context. I was merely trying to explain the semantics of the phrase "char **argv", where it is a parameter in main()'s parameter list, because that is what you asked. Read my post again.
    How is it in an inapplicable context?
    I am trying to say if I had parameters in the command line
    DirFile -K "......"
    would *argv point to the first element to each of the arguments?
    Being able to understand type declarations is an important part of C literacy.
    Yeah, I am still learning..just too slowly.
    You ended that sentence with a preposition...Bastard!

  5. #35
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by whiteflags View Post
    Yeah but his post just read "if I were going to change [the strings], I would create a local variable instead," implying that parameters aren't local. Hence, my confused reply.
    The reason is that the caller should not care whether to create a copy or not. It's callee responsibility to make a copy if necessary. You might write a better code later, which does not need a copy and the callers will still be making ones.
    Yet another example: you might mark this function as deprecated and transfer its job to another function, like this:

    Code:
    void GetFiles(string Folder)
    {
        AcquireFilesFrom(Folder);
    }
    It will produce an additional, useless copy.
    I never put signature, but I decided to make an exception.

  6. #36
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,750
    The reason is that the caller should not care whether to create a copy or not. It's callee responsibility to make a copy if necessary. You might write a better code later, which does not need a copy and the callers will still be making ones.
    Yet another example: you might mark this function as deprecated and transfer its job to another function, like this:
    Well if the function is deprecated, you wouldn't change it's signature anyway. I don't see a point in simulating pass by value semantics if that is what we need in the first place. AFAICT the only reason is because you want to support a particular interface.
    Last edited by whiteflags; 01-20-2011 at 09:59 AM.

  7. #37
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    [QUOTE=Eman;997083]How is it in an inapplicable context?
    I am trying to say if I had parameters in the command line
    DirFile -K "......"
    would *argv point to the first element to each of the arguments?
    [quote]

    For example: DirToFile -K fred.txt c:\

    Each of the pointers in the arg[] array would point to the first character of each parameter.

  8. #38
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    I see what you mean by "context".
    I have been doing some googling (why is it always pointers!!! grrr!)
    so
    void foo(char *a[]) is the same as
    void foo(char **a)
    and it happens only during function calls.

    But in local access it will not decay to a pointer, but will act like one. hence
    char a[]="Hello"
    char **s = a would not work.

    It is a bit confusing but I think I can live with it lol.
    You ended that sentence with a preposition...Bastard!

  9. #39
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,750
    Anyway

    foo(char **a);
    is just a convenient way to write
    foo(char *a[]);

    Any arguments will still experience decay and become pointer types. You're just making mountains out of mole hills.

  10. #40
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    yeah cool. But what is the point of passing arguments to main?
    I have seen it used in tutorials, but never been taught what it meant or even used it at all. So I ignore using it as well.
    You ended that sentence with a preposition...Bastard!

  11. #41
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    Do you ever use console? Compiling code:
    Code:
    g++ -ansi -pedantic -Wall -Wextra mycode.cpp
    is an example of using those arguments.

    If in your application you never care about those arguments, then by all means don't use them. (-Wunused will cause compiler to spit a warning about unused variables).

  12. #42
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Do you ever use console? Compiling code:
    xD not to compile, just to run whenever i forget to put getchar() or cin.get()


    Code:
    g++ -ansi -pedantic -Wall -Wextra mycode.cpp
    so this arguments are passed to main() through pointers..and i would guess I am able to access them
    cout<<argv[0] and so on.
    I don't see the use of saving the compile code to the stack. I blame my naivety...
    You ended that sentence with a preposition...Bastard!

  13. #43
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eman View Post
    yeah cool. But what is the point of passing arguments to main?
    I have seen it used in tutorials, but never been taught what it meant or even used it at all. So I ignore using it as well.
    Aassuming you're on Windows ...
    Type Win+R ... in the run dialog put CMD and hit enter.
    In the console window, type DIR C:\ and press enter

    Do you see it now? You just ran DIR with a parameter (C:\) passed in on it's command line. DIR would be useless without it... This would be true of any console program that doesn't go out of it's way to get user input.

    For example; if you compiled my little proggy, assuming you called it DirToFile...

    Find DirToFile.exe and (temporarily) move it to C:\ ...

    Now in that command shell type the following sequence...
    Code:
    C:
    CD \
    
    DirToFile test.txt "Program Files"
    
    DIR
    You should see a file test.txt that was created.
    when you open the file it should contain a list of all the folders under Program Files...

    When writing the program I had three choices...
    1) Accept the parameters from the command line
    2) Ask the user for the parameters inside the program
    3) Ask the user if the command line is blank

    I simply made a choice to do it the first way. First because it's easier for the user; they have one line to type and instant results after hitting enter... Second because it's a little bit easier to code... Third because it follows the general paradyme of Command Line Interface (CLI) programs in Windows and other operating systems... and Fourth because if it does nothing without it's command line, it eliminates the errors that may be caused by the user clicking on it with their mouse.

    BTW... if you use the DirToFile program with the -R "switch" it has a practical application... you can make playlists for your media player with it... If you open the command shell and switch to a folder with music files in it... lets say it's C:\My Music\MP3 for this example...

    Do the WIN+R thing... type CMD click OK... in the console window type...
    Code:
    C:
    CD \
    DirToFile -R Playlist.m3u "c:\My Music\MP3"
    Now in c:\ click on the Playlist.m3u file and your Media Player should open and play those files for you... The -R simply scrambles the order so you don't hear the same playlist every time.

    Also... All Windows Programs have to accept parameters on their command lines. This is done so that when you click on a file the Windows "file associations" data base can find the correct program to run for that file, add the filename to the command line and run the program for you... Any windows program that does not accept filenames on it's command line cannot be associated with any file type.
    Last edited by CommonTater; 01-20-2011 at 11:42 AM. Reason: afterthought

  14. #44
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    Quote Originally Posted by Eman View Post
    I don't see the use of saving the compile code to the stack. I blame my naivety...
    Could you elaborate on that?

  15. #45
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    @CommonTater, I don't know why but I can't compile your code..it says unresolved reference to PathUnquotesA..
    And what your program does sounds really cool

    Quote Originally Posted by Xupicor View Post
    Could you elaborate on that?
    when every function is called, it saves its parameters to the stack.
    So the main(int argc, char **argv) saves the number of parameters and the address of the first element (of pointers) to the stack. So I should be able to do a loop and cout (I say cout because I don't know how else it could be used) the names of the parameters.
    I don't see the point of accessing the arguments in main, and i think it is because I don't know the use of it.

    But CommonTater just showed how useful it is.
    You ended that sentence with a preposition...Bastard!

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

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