Need help understanding how I can send in integers at command prompt

This is a discussion on Need help understanding how I can send in integers at command prompt within the C Programming forums, part of the General Programming Boards category; I need help understanding how I can send in integers at command prompt, at which that array of integers will ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Need help understanding how I can send in integers at command prompt

    I need help understanding how I can send in integers at command prompt, at which that array of integers will then be sent to functions to work with.

    I know I need to include the stdarg.h library but I am unsure of how I can send in the integers because
    Code:
    va_start
    expects the list variable and the last variable before the new list, but I dont have any previous variable?

    I just want to do this
    Code:
    test.exe 1020
    , 1020 will then be sent to a function etc.

    Appreciate any claification, thanks

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Hopefully, this is what you need....

    Bob

    Code:
    #include <stdio.h>
    #include <stdlib.h> // Needed for atoi
    int main(int argc, char **argv)
    {
        int iInput;
        if(argc >1)
        {
            iInput = atoi(argv[1]);
            printf("iInput = %d\n", iInput);
        }
        return 0;
    }

  3. #3
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    Code:
    int *p = malloc(sizeof(int) * (argc - 1));
    int i = 1;
    
    for (; i<argc; i++)
        p[i-1] = atoi(argv[i]);
    
    myFunc(p);
    
    free(p);
    call this code with:

    Code:
    test.exe 1 2 3 4 5 6 7

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    oh I guess I misunderstood, I thought he wanted to send an array of ints.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    The above can be done without a need for va_arg,va_start,va_end?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    yes -- argc is the number of arguments on the command-line and argv is an array of the strings. argc will always be at least one because the first argument is always the name of the program.

  7. #7
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Ancient Dragon
    yes -- argc is the number of arguments on the command-line and argv is an array of the strings. argc will always be at least one because the first argument is always the name of the program.
    Generally, but not always. The standard allows for argc to be 0, and thus argv[0] is NULL.

  8. #8
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    if argc is 0 then argv[0] should be invalid. But anway, you're confusing the poor boy. INFERNO2K, if you want to enter more than 1 parameter as an int, then use my sample. if you just want a single number as a parameter, use bob's.

  9. #9
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Not sure what you mean by "invalid". Like I said above, if argc is 0, then argv[0] is NULL, because argv[argc] is always NULL, the standard says so.

    If I have to make a choice between correcting an incorrect post and not risking confusing someone, I'll have to choose the former.
    Last edited by cwr; 10-24-2005 at 11:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. tuncated uint8_t
    By Coens in forum C Programming
    Replies: 14
    Last Post: 11-24-2008, 06:57 AM
  3. send() characters from integers
    By joecaveman in forum C Programming
    Replies: 2
    Last Post: 05-17-2005, 07:38 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM

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