in command line argument
i am writing argument for the main function
as follows

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

here argc and argv is cleared to me but why we are using char* with argv... means why we are using * ???

Recommended Answers

All 7 Replies

argc is an int representing the number of arguments in argv. argv is an array of pointers to the arguments the program expects. If you don't plan on using them they can be omitted. Here's a more involved explanation

Also, argv should be a const char* not a char*. There are two main ways to process command line arguments. One is to loop from 1 to argc and process each argument (old style). The other is to call the function getopt() for each supported argument. These days, the preferred method is to use getopt(), though I must confess to usually using the old style, simply because of 30 years of bad habits! :-)

Also, argv should be a const char* not a char*.

No, it should be char* to be strictly conforming. Did you know that the strings in argv are modifiable?

The other is to loop through from 1 to argc and call the function getopt().

getopt() is not a standard function though.

I must confess to usually using the old style, simply because of 30 years of bad habits! :-)

I usually use an ad hoc method as well. Often I don't need all of the baggage that comes along with getopt(), just a simple parsing of argv.

  1. Yes, I know they (argv strings) are modifiable, but IMO not a good idea! It is easy to force a buffer overrun, creating a potential security breach! :-)

  2. The getopt() function is a POSIX standard. From the Linux man pages:

    Base Definitions volume of IEEE Std 1003.1-2001, <unistd.h>, the Shell and Utilities volume of IEEE Std 1003.1-2001

  3. I agree! :-)

Yes, I know they (argv strings) are modifiable, but IMO not a good idea! It is easy to force a buffer overrun, creating a potential security breach! :-)

Agreed, certainly. I've never encountered a situation where it was necessary to modify one of the argv strings, and honestly I agree that it's a bug waiting to happen. However, you don't have much flexibility in the signature of main(). Equivalent types are allowed, but char* and const char* are NOT equivalent types. So there's no guarantee that the compiler will accept this definition of main():

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

If it does, all is well (such support is implementation-defined), but the code won't be portable. If it doesn't, you've invoked undefined behavior just as readily as using void main() on a compiler that doesn't support a void return type.

The getopt() function is a POSIX standard.

Which is wholely irrelevant when talking about portable C++. The world is not all POSIX, after all. ;)

is there any difference between char* and const char*

Yes a char* is a pointer to a char that can be modified. a const char* is a char pointer that cant be modified.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.