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

ifjake

macrumors 6502a
Original poster
Jan 19, 2004
563
2
I'm currently taking a beginners C++ course that apparently uses outdated standards and such, and I'm trying to reconcile the ways of doing things as we are taught in class with the more up-to-date ways of doing things. Previously I figured out how the .h-ending header files were different than the non-.h ones and how for example with iostream you have to add a using namespace std; line for cout statements to look normal. I'm still figuring out what that means exactly, but I can get it to work so I'm not that concerned.

What I'm curious about now is the arguments within the main function header as put there by default when starting a C++ command line project: main(int argc, char * const argv[]). What the heck are these for? What "topic" would I look under to figure out more about this?
 
What I'm curious about now is the arguments within the main function header as put there by default when starting a C++ command line project: main(int argc, char * const argv[]). What the heck are these for? What "topic" would I look under to figure out more about this?

The parameters in the main() function represent values passed to your program from the command line. It's been a long time since I did C++ so bear with me; I think "int argc" is the number of arguments, and the char array is an array of the command line values.
 
The parameters in the main() function represent values passed to your program from the command line. It's been a long time since I did C++ so bear with me; I think "int argc" is the number of arguments, and the char array is an array of the command line values.

argc is the number of arguments. argv is a array of pointers to strings (i.e. an array of char arrays), each of which is one argument. Not saying you were wrong, just clarifying for the person that was asking =)
 
Also bear in mind that the first entry in argv (offset=0) is the name of the program itself so if you call you program like this:

>cppprogram arg1 15 2.67

argc will be 4 and argv[] will be {"cppprogram", "arg1", "15", "2.67"}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.