argv issues

Discussion in 'Mac Programming' started by dukebound85, Oct 31, 2006.

  1. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #1
    Hi all, I have a program that prints out the first 15 elements of an array. So far I have it so I set to a variable that is the starting pt. For example, say I have it as,

    int num=10 the the array will print out 10-24 on each on its own line starting with num or 10.

    Now I am trying to implement the command line. How do I go about setting num=argv[1] for example where argv[1] is the first argument i enter as an integer that i want the array to start at when printing out?

    This is what i enter:

    program 15 20


    and i want it to print 2 arrays, one starting at 15 and the other at 20 for instance . I have the loops down but am having trouble converting that argv to a integer if you know what i mean.

    Any ideas?

    i tried int num=(int)argv[1] but this wouldn't compile nicely

    thanks for any help you guys can offer on this
     
  2. cbougher macrumors member

    Joined:
    Oct 19, 2006
    Location:
    Atlanta, GA
    #2
    Try
    atoi(argv[1]);

    etc...

    be sure to add
    #include <stdlib.h>
     
  3. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #3
    In case that wasn't enough guidance, here's more of a skeleton:
    Code:
    int main(int argc, char **argv) {
    
    int firstvalue, secondvalue ;
    
    /* check that there are two command arguments */
    if ( argc != 3 ) {
        /* wrong number of arguments - print a message and exit */
        }
    
    firstvalue = atoi(argv[1]) ;
    secondvalue = atoi(argv[2]) ;
    
    /* do something with these two values */
    
    }
    Note that argc includes the name of the program (which is in argv[0]), so when you supply two more arguments on the command line that makes argc equal to 3. Note also that these two forms are equivalent:
    Code:
    char **argv
    char *argv[]
     
  4. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #4
    thanks alot, however, I still have a problem
    in my main method i have those variable declared to the atoi[arg's]

    first=atoi(argv[1]);

    However, in my main i have a function call that needs to make use of the value first

    How do I reference this value if I am not in the main function? I tried to put first=atoi(argv[1]); in the function but it did not work

    a skeleton of what i have is this

    main(int argc, char *argv[])
    {

    ...
    ...
    int first;
    first=atoi(argv[1]);

    ...
    ...
    function(array)

    ...
    return 0;
    }

    void function(int array[])
    {
    int first;
    ...
    ...
    }

    however first is not the same value as is in main how can i change this?


    thanks for the help, it really is appreciated
     
  5. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #5
    There are two approaches you could take:
    1. Make first a global variable so it belongs to the whole program, not to any specific function.
    2. Pass first from the main program to the function to they each have a copy of it.
    Method #1 is the easy approach, although some people frown on the use of global functions because in a large program their scope is not limited to the functions that actually use them, making it more likely that another function will "mess with them". Method #2 is therefore the tidy approach, and better if the program will become large or someone will be rating or studying it for style.

    Method #1:
    Code:
    
    int first;
    
    main(int argc, char *argv[])
    {
    
    ...
    ...
    first=atoi(argv[1]);
    
    ...
    ...
    function(array);
    
    ...
    return 0;
    }
    
    void function(int array[])
    {
    /* We removed the declaration of first here. */
    /* If we had a declaration here, */
    /* it would override the global variable and create a new local variable. */
    
    ...
    whatever=first ;
    ...
    }
    Method #2:
    Code:
    
    main(int argc, char *argv[])
    {
    
    ...
    ...
    int first;
    first=atoi(argv[1]);
    
    ...
    ...
    function(array,first);
    
    ...
    return 0;
    }
    
    void function(int array[],int first)
    {
    ...
    whatever=first;
    ...
    }
    With method #2 you don't have to name the function argument "first". It could be called anything at all, as long as the declaration and use agree. This might actually be a better way to code it, because it emphasizes that there are really two variables involved, the one named first in the main program and a second variable that exists only with in the function. Here's method #2 using another name:
    Code:
    
    main(int argc, char *argv[])
    {
    
    ...
    ...
    int first;
    first=atoi(argv[1]);
    
    ...
    ...
    function(array,first);
    
    ...
    return 0;
    }
    
    void function(int array[],int myvalue)
    {
    ...
    whatever=myvalue;
    ...
    }
    If you assigned a value to myvalue in the function, it would not affect the value of first in the main program, so that's a good reason to give them different names.
     
  6. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #6
    thanks Dr Q

    I have gotten everything to work except for this function

    void Hex(int num )
    {
    int q;
    int r;
    int spot = 0;
    while( num > 0 )
    {
    q = num /16;
    r= num % 16;
    if( r > 9 )
    {
    hexValue[spot++] = (char )('A' +(r-10));
    }
    else
    hexValue[spot++] = (char)(48 + r);
    num=num/16;
    }


    however the output for say 20 is 41 in hex where it should be 14. any ideas whats going on?
     
  7. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #7
    It's doing exactly what you told it :D When you use the division and remainder method to convert, the digits fall out in reverse.

    You could reverse the string in place before returning.

    Or, you could write the characters backwards (you would need to shift a string pointer over to keep track of the beginning, or you could shuffle all the characters leftward inside the function before returning).

    Or, you could use a different algorithm that finds the digits in the order you want them. This method would do.
     
  8. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #8
    Thanks,
    how would I go about this exactly?
     
  9. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003

Share This Page