Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
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
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
40,077
8,337
Los Angeles
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[]
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
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
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
40,077
8,337
Los Angeles
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.
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
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?
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
however the output for say 20 is 41 in hex where it should be 14. any ideas whats going on?
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.