- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Passing an integer argument to a C program on comm...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-04-2009 06:39 PM
тАО08-04-2009 06:39 PM
A bit off-topic but I *am* using GCC on a Linux box. :-)
I have written a program, that at startup, I would like to enter an integer on the command line and pass it to the program.
All I can find is the ARGV and that only appears to pass an array of pointers.
int main(int argc, char *argc[])
Any ideas?
(Oh - I came up with a work-around which is taking the pointer and doing a STRCMP with a known value and if there is a match setting the variable to the integer I would have put on the command line. I just can't believe that with all the flexibility of C that there isn't a better way. I *DID* look thru a lot of my C programming books but could not any reference).
regards,
tonyp (aka hunybuny)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-04-2009 07:05 PM
тАО08-04-2009 07:05 PM
SolutionI'm not, but:
alp $ type nil.c
#include
#include
main( int argc, char **argv)
{
long val;
if (argc > 1)
{
val = strtol( argv[ 1], NULL, 10);
printf( " val = %ld.\n", val);
}
}
alp $ cc nil
alp $ link nil
alp $ exec nil
alp $ exec nil 29 30 31
val = 29.
alp $ cc /version
HP C V7.3-009 on OpenVMS Alpha V7.3-2
Seems to work with a GCC somewhere, too:
sol# gcc -o nil nil.c
sol# ./nil
sol# ./nil 19 20 21
val = 19.
sol# uname -a
SunOS sol 5.10 Generic_137137-09 sun4u sparc sun4u
sol# gcc --version
gcc (GCC) 3.4.6
[...]
The program gets a string, but, given a
string, many things are possible.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-04-2009 10:59 PM
тАО08-04-2009 10:59 PM
Re: Passing an integer argument to a C program on command line
See also getopt(): it is a bit complicated but makes it easy to parse a large number of options in any order.
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-05-2009 08:40 AM
тАО08-05-2009 08:40 AM
Re: Passing an integer argument to a C program on command line
Thank you for the help.
The suggestion works perfectly!
regards,
tonyp (aka hunybuny)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-10-2009 10:30 AM
тАО08-10-2009 10:30 AM
Re: Passing an integer argument to a C program on command line
Problem Resolved.
regards,
tonyp (aka hunybuny)