Determining Number of Arguments argv

Discussion in 'C Programming' started by Owner, Apr 7, 2011.

  1. Owner

    Owner Guest

    Can this code determine number of aruments?

    sizeof argv / sizeof argv[0]

    I'd appreciate any comments
    Owner, Apr 7, 2011
    #1
    1. Advertising

  2. Owner

    Lew Pitcher Guest

    On April 7, 2011 15:51, in comp.lang.c, wrote:

    > Can this code determine number of aruments?
    >
    > sizeof argv / sizeof argv[0]
    >
    > I'd appreciate any comments


    Assuming that you use argv in the sense of the 2nd argument to main()
    i.e.
    char **argv;
    or
    char *argv[];

    then, NO]
    sizeof argv / sizeof argv[0]
    does not give you the number of arguments in the argv[] array.

    It /does/ give you
    the number of char * pointers that can fit in a char ** pointer;
    a singularly useless tidbit of information.


    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
    Lew Pitcher, Apr 7, 2011
    #2
    1. Advertising

  3. Owner

    Ian Collins Guest

    On 04/ 8/11 07:51 AM, Owner wrote:
    > Can this code determine number of aruments?
    >
    > sizeof argv / sizeof argv[0]


    No. sizeof(argv) is the size of a char**.

    What's wrong with using argc?

    --
    Ian Collins
    Ian Collins, Apr 7, 2011
    #3
  4. Owner

    Ian Collins Guest

    On 04/ 8/11 07:56 AM, Lew Pitcher wrote:
    > On April 7, 2011 15:51, in comp.lang.c, wrote:
    >
    >> Can this code determine number of aruments?
    >>
    >> sizeof argv / sizeof argv[0]
    >>
    >> I'd appreciate any comments

    >
    > Assuming that you use argv in the sense of the 2nd argument to main()
    > i.e.
    > char **argv;
    > or
    > char *argv[];
    >
    > then, NO]
    > sizeof argv / sizeof argv[0]
    > does not give you the number of arguments in the argv[] array.
    >
    > It /does/ give you
    > the number of char * pointers that can fit in a char ** pointer;
    > a singularly useless tidbit of information.


    No, it gives you the size of a char**.

    --
    Ian Collins
    Ian Collins, Apr 7, 2011
    #4
  5. Owner

    Owner Guest

    On Thu, 07 Apr 2011 15:51:40 -0400, Owner wrote:

    > Can this code determine number of aruments?
    >
    > sizeof argv / sizeof argv[0]
    >
    > I'd appreciate any comments


    I need to build a copy of argv in main, so before that

    need to determine how many arguments are there, so I can do

    this

    char *newargv["number of arguments"];

    char *newargv[argc] didn't work. it says it has to be

    constant expression

    sizeof argv did give me size of a pointer which is 4
    Owner, Apr 7, 2011
    #5
  6. Owner

    Dr Nick Guest

    Owner <> writes:

    > On Thu, 07 Apr 2011 15:51:40 -0400, Owner wrote:
    >
    >> Can this code determine number of aruments?
    >>
    >> sizeof argv / sizeof argv[0]
    >>
    >> I'd appreciate any comments

    >
    > I need to build a copy of argv in main, so before that
    >
    > need to determine how many arguments are there, so I can do
    >
    > this
    >
    > char *newargv["number of arguments"];
    >
    > char *newargv[argc] didn't work. it says it has to be
    >
    > constant expression
    >
    > sizeof argv did give me size of a pointer which is 4


    It does (pre-C99) have to be a constant expression. But, if you think
    about it, the number of arguments can't be a constant expression - your
    program can be run with any number (up to some system dependent limit
    perhaps) of arguments.

    So you either need to use a compiler that char *newargv[argc] works on,
    or do something with malloc and free, or use a not-quite-standard thing
    like alloca to get yourself some space.
    --
    Online waterways route planner | http://canalplan.eu
    Plan trips, see photos, check facilities | http://canalplan.org.uk
    Dr Nick, Apr 7, 2011
    #6
  7. Ian Collins <> wrote:

    > On 04/ 8/11 07:56 AM, Lew Pitcher wrote:
    >> Assuming that you use argv in the sense of the 2nd argument to
    >> main() i.e.
    >> char **argv;
    >> or
    >> char *argv[];
    >>
    >> then, NO]
    >> sizeof argv / sizeof argv[0]
    >> does not give you the number of arguments in the argv[] array.
    >>
    >> It /does/ give you
    >> the number of char * pointers that can fit in a char **
    >> pointer;
    >> a singularly useless tidbit of information.

    >
    > No, it gives you the size of a char**.


    How that? Just one possible example may be sizeof(char**) = 4
    and sizeof(char*) = 4. In my math the result of the division
    should be 1.

    -- Ralf
    Ralf Damaschke, Apr 7, 2011
    #7
  8. "Dr Nick" wrote:
    > So you either need to use a compiler that char *newargv[argc] works on,
    > or do something with malloc and free, or use a not-quite-standard thing
    > like alloca to get yourself some space.


    Also, it should be char *newargv[argc + 1]; recall that argv[arc] is defined.

    --Joel
    Joel C. Salomon, Apr 7, 2011
    #8
  9. Owner

    Lew Pitcher Guest

    On April 7, 2011 16:26, in comp.lang.c, wrote:

    > On Thu, 07 Apr 2011 15:51:40 -0400, Owner wrote:
    >
    >> Can this code determine number of aruments?
    >>
    >> sizeof argv / sizeof argv[0]
    >>
    >> I'd appreciate any comments

    >
    > I need to build a copy of argv in main, so before that
    >
    > need to determine how many arguments are there, so I can do
    >
    > this
    >
    > char *newargv["number of arguments"];
    >
    > char *newargv[argc] didn't work.


    Yah. What you want is either
    a VLA (variable-length array), which is available in C99,
    or
    a malloc()ed bit of memory, which is available in all C standards.



    Assuming malloc(), you want something like...

    char **newargv;

    if ((newargv = malloc((argc + 1) * sizeof(char *)) == NULL)
    exit(EXIT_FAILURE); /* or some other exception action */


    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
    Lew Pitcher, Apr 7, 2011
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trevor M. Lango

    Determining Number of Arguments

    Trevor M. Lango, Feb 19, 2004, in forum: C++
    Replies:
    3
    Views:
    405
    Rolf Magnus
    Feb 20, 2004
  2. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,584
    Richard Heathfield
    Sep 3, 2003
  3. David
    Replies:
    10
    Views:
    5,951
    Richard Heathfield
    Sep 15, 2003
  4. Darren Dale
    Replies:
    68
    Views:
    1,138
    Alex Martelli
    Nov 21, 2004
  5. Jp Calderone
    Replies:
    4
    Views:
    467
    Peter Otten
    Nov 16, 2004
Loading...

Share This Page