Thread: Is this correct?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    64

    Is this correct?

    I read this in a book, do you think it correct, or just a typing mistake?

    hint: double asterisks

    char **argv
    argv: pointer to char

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    The code is right. The description is not. argv is an array of pointer to char.

  3. #3
    Quote Originally Posted by thinhare
    I read this in a book, do you think it correct, or just a typing mistake?

    hint: double asterisks
    The comment doesn't match the code.
    Emmanuel Delahaye

    "C is a sharp tool"

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    so what's the right comment?

  5. #5
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    To be completely accurate, it should say it's a pointer to a pointer of type char. Or more simply, a pointer to a string.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    frankly speaking, I think it's more likely the upper line which has been mis-typed. what do you think? if this is the case, how would you correct it to fit the description beneath?

    (it is from the book written by Brian W. Kernighan and Dennis M. Ritchie.)

  7. #7
    Quote Originally Posted by thinhare
    (it is from the book written by Brian W. Kernighan and Dennis M. Ritchie.)
    Please quote the edition and page/line number.
    Emmanuel Delahaye

    "C is a sharp tool"

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    I've proven that both the code and the comment are correct.
    #include <stdio.h>

    main(int argc, char *argv[]) {
    printf("%c\n", **argv);
    }
    It does print a CHAR. What do you think?

    Emmanuel Delaha:
    "The C Programming Language, Second Edition"
    Chapter 5.12 - "Complicated Declarations"

  9. #9
    Quote Originally Posted by thinhare
    I've proven that both the code and the comment are correct.

    It does print a CHAR. What do you think?
    It's correct, but it prooves that

    **argv is a char
    *argv is pointer to char (char *)
    argv is a pointer to a pointer to char (char **)

    "The C Programming Language, Second Edition"
    Chapter 5.12 - "Complicated Declarations"
    Please give the line number (I don't have the book at home). The errata list is here:

    http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html
    Last edited by Emmanuel Delaha; 12-24-2004 at 12:39 PM.
    Emmanuel Delahaye

    "C is a sharp tool"

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    what do you find in the same book of yours?

    I'd thought it was
    char *argv
    which was mistyped with one more asterisk.

    but considering it's in the section of complicated declarations, that is, something got to be complicated than usual, I became not so certain.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    argv is whatever you want it to be....by convention it is typically declared in main's parameter list as char **argv or *argv[].

    **argv, argv[0][0], and *argv[0] are all the same....(for the sake of this discussion).
    argv[1][0] refers to the second string of the parameter list - first character.
    argv[1][5] refers to the fifth character - second string of the parameter list.
    **argv refers to argv[0][0] -- first string, first character
    argv[1][0] refers to the second string of the parameter list.
    *argv[0] = **argv = argv[0][0]
    *argv[2] refers to the third character of the first string
    all of the above can have the value NULL


    one thing i don't know however
    ((argv+1) == &argv[1][0] ) = (true || false)?
    Last edited by misplaced; 12-24-2004 at 01:05 PM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok to understand this better, and I'm not sure why no one brought this up yet, here is what is being done:

    Example:
    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv) {
      int i;
    
      for(i = 0; i < argc; i++)
        printf("command line argument %02d: \"%s\"", i, argv[i]);
    
      return 0;
    }
    Ok now compile and run this code. Ok it didn't do a whole lot. Now try something different like (calling the binary program in my examples):

    Code:
    program arg1 arg2 arg3
    Then how about:

    Code:
    program hello world. this is a pointless program
    Ok now you will notice that it outputs each space separated string as a command line arg. Just for fun:

    Code:
    program "hello world. this is a pointless program"
    That time you will find that it output the entire quote encased string as one argument. Anyway the point is that in the above argv is going to be an unknown sized array of strings. The size of the array will be known at run-time. That is what argc indicates. But as far as compile time there is no determined size. Why use char **? Because remember that we are not using an unknown lengthed string, we are using an array of strings that has an unpredetermined size. So instead of having something like this:

    Example:
    Code:
    char argv[] = { 'a', 'b', 'c' };
    We have something more like this:

    Example:
    Code:
    char *argv[] = { "program", "arg1", "arg2" };

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >"The C Programming Language, Second Edition"
    >Chapter 5.12 - "Complicated Declarations"
    It turns out to be a typing mistake, but on your part, not the book's. My copy of K&R2 says this:
    Code:
    char **argv
        argv: pointer to pointer to char
    My best code is written with the delete key.

  14. #14
    Quote Originally Posted by Prelude
    >"The C Programming Language, Second Edition"
    >Chapter 5.12 - "Complicated Declarations"
    It turns out to be a typing mistake, but on your part, not the book's. My copy of K&R2 says this:
    Code:
    char **argv
        argv: pointer to pointer to char
    He he! My point exactly!
    Emmanuel Delahaye

    "C is a sharp tool"

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    OK. I got it. Thank you all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Is this correct : passing strings?
    By socket in forum C Programming
    Replies: 15
    Last Post: 11-25-2008, 02:03 PM
  3. correct order to insert new node?
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2004, 07:51 PM
  4. Replies: 1
    Last Post: 05-26-2004, 12:58 AM
  5. Loop until enter correct value
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 01:23 AM