C++ editing the elements of char* argv[] - c++

All of you know this function:
int main(int argc, char* argv[])
{
}
I want to write a command line interface in Linux for my program, which is usually done by getopt_long()
My program would be executed from command line like this:
pop3 get --limit 25 --recent
Hence, the argv[] would include pop3 as its program name, and the rest are treated as options.
I want to delete pop3 from my string and set the first token after it as the first element of the array. Is there a way to do that other than looping through?

Increment the argv pointer, and decrement the argc.
Example:
int main(int argc, char *argv[])
{
argc--;
argv++;
return 0;
}
This works, because when you increment argv, you still have the previous data in memory, it's just that the base address of the argv has increased. And you decrement argc, because you now have one less argument.

Related

How to add multiple command line parameters to an executable C++ [duplicate]

In many C++ IDE's and compilers, when it generates the main function for you, it looks like this:
int main(int argc, char *argv[])
When I code C++ without an IDE, just with a command line compiler, I type:
int main()
without any parameters. What does this mean, and is it vital to my program?
argv and argc are how command line arguments are passed to main() in C and C++.
argc will be the number of strings pointed to by argv. This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.
The variables are named argc (argument count) and argv (argument vector) by convention, but they can be given any valid identifier: int main(int num_args, char** arg_strings) is equally valid.
They can also be omitted entirely, yielding int main(), if you do not intend to process command line arguments.
Try the following program:
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Have " << argc << " arguments:" << std::endl;
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
}
Running it with ./test a1 b2 c3 will output
Have 4 arguments:
./test
a1
b2
c3
argc is the number of arguments being passed into your program from the command line and argv is the array of arguments.
You can loop through the arguments knowing the number of them like:
for(int i = 0; i < argc; i++)
{
// argv[i] is the argument at index i
}
Suppose you run your program thus (using sh syntax):
myprog arg1 arg2 'arg 3'
If you declared your main as int main(int argc, char *argv[]), then (in most environments), your main() will be called as if like:
p = { "myprog", "arg1", "arg2", "arg 3", NULL };
exit(main(4, p));
However, if you declared your main as int main(), it will be called something like
exit(main());
and you don't get the arguments passed.
Two additional things to note:
These are the only two standard-mandated signatures for main. If a particular platform accepts extra arguments or a different return type, then that's an extension and should not be relied upon in a portable program.
*argv[] and **argv are exactly equivalent, so you can write int main(int argc, char *argv[]) as int main(int argc, char **argv).
int main();
This is a simple declaration. It cannot take any command line arguments.
int main(int argc, char* argv[]);
This declaration is used when your program must take command-line arguments. When run like such:
myprogram arg1 arg2 arg3
argc, or Argument Count, will be set to 4 (four arguments), and argv, or Argument Vectors, will be populated with string pointers to "myprogram", "arg1", "arg2", and "arg3". The program invocation (myprogram) is included in the arguments!
Alternatively, you could use:
int main(int argc, char** argv);
This is also valid.
There is another parameter you can add:
int main (int argc, char *argv[], char *envp[])
The envp parameter also contains environment variables. Each entry follows this format:
VARIABLENAME=VariableValue
like this:
SHELL=/bin/bash
The environment variables list is null-terminated.
IMPORTANT: DO NOT use any argv or envp values directly in calls to system()! This is a huge security hole as malicious users could set environment variables to command-line commands and (potentially) cause massive damage. In general, just don't use system(). There is almost always a better solution implemented through C libraries.
The parameters to main represent the command line parameters provided to the program when it was started. The argc parameter represents the number of command line arguments, and char *argv[] is an array of strings (character pointers) representing the individual arguments provided on the command line.
The main function can have two parameters, argc and argv. argc is an integer (int) parameter, and it is the number of arguments passed to the program.
The program name is always the first argument, so there will be at least one argument to a program and the minimum value of argc will be one. But if a program has itself two arguments the value of argc will be three.
Parameter argv points to a string array and is called the argument vector. It is a one dimensional string array of function arguments.
Lets consider the declaration:
int main (int argc, char *argv[])
In the above declaration, the type of the second parameter named argv is actually a char**. That is, argv is a pointer to a pointer to a char. This is because a char* [] decays to a char** due to type decay. For example, the below given declarations are equivalent:
int main (int argc, char *argv[]); //first declaration
int main (int argc, char **argv); //RE-DECLARATION. Equivalent to the above declaration
In other words, argv is a pointer that points to the first element of an array with elements of type char*. Moreover, each elements argv[i] of the array(with elements of type char*) itself point to a character which is the start of a null terminated character string. That is, each element argv[i] points to the first element of an array with elements of type char(and not const char). A diagram is given for illustration purposes:
As already said in other answers, this form of declaration of main is used when we want to make use of the command line argument(s).
The first parameter is the number of arguments provided and the second parameter is a list of strings representing those arguments.
Both of
int main(int argc, char *argv[]);
int main();
are legal definitions of the entry point for a C or C++ program. Stroustrup: C++ Style and Technique FAQ details some of the variations that are possible or legal for your main function.
In case you learn something from this
#include<iostream>
using namespace std;
int main(int argc, char** argv) {
cout << "This program has " << argc << " arguments:" << endl;
for (int i = 0; i < argc; ++i) {
cout << argv[i] << endl;
}
return 0;
}
This program has 3 arguments. Then the output will be like this.
C:\Users\user\Desktop\hello.exe
hello
people
When using int and char**, the first argument will be the number of commands in by which the programs is called and second one is all those commands
Just to add because someone says there is a third parameter (*envp[]), it's true, there is, but is not POSIX safe, if you want your program to use environment variables you should use extern char environ ;D

Using argv[1] to load an image [duplicate]

In many C++ IDE's and compilers, when it generates the main function for you, it looks like this:
int main(int argc, char *argv[])
When I code C++ without an IDE, just with a command line compiler, I type:
int main()
without any parameters. What does this mean, and is it vital to my program?
argv and argc are how command line arguments are passed to main() in C and C++.
argc will be the number of strings pointed to by argv. This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.
The variables are named argc (argument count) and argv (argument vector) by convention, but they can be given any valid identifier: int main(int num_args, char** arg_strings) is equally valid.
They can also be omitted entirely, yielding int main(), if you do not intend to process command line arguments.
Try the following program:
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Have " << argc << " arguments:" << std::endl;
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
}
Running it with ./test a1 b2 c3 will output
Have 4 arguments:
./test
a1
b2
c3
argc is the number of arguments being passed into your program from the command line and argv is the array of arguments.
You can loop through the arguments knowing the number of them like:
for(int i = 0; i < argc; i++)
{
// argv[i] is the argument at index i
}
Suppose you run your program thus (using sh syntax):
myprog arg1 arg2 'arg 3'
If you declared your main as int main(int argc, char *argv[]), then (in most environments), your main() will be called as if like:
p = { "myprog", "arg1", "arg2", "arg 3", NULL };
exit(main(4, p));
However, if you declared your main as int main(), it will be called something like
exit(main());
and you don't get the arguments passed.
Two additional things to note:
These are the only two standard-mandated signatures for main. If a particular platform accepts extra arguments or a different return type, then that's an extension and should not be relied upon in a portable program.
*argv[] and **argv are exactly equivalent, so you can write int main(int argc, char *argv[]) as int main(int argc, char **argv).
int main();
This is a simple declaration. It cannot take any command line arguments.
int main(int argc, char* argv[]);
This declaration is used when your program must take command-line arguments. When run like such:
myprogram arg1 arg2 arg3
argc, or Argument Count, will be set to 4 (four arguments), and argv, or Argument Vectors, will be populated with string pointers to "myprogram", "arg1", "arg2", and "arg3". The program invocation (myprogram) is included in the arguments!
Alternatively, you could use:
int main(int argc, char** argv);
This is also valid.
There is another parameter you can add:
int main (int argc, char *argv[], char *envp[])
The envp parameter also contains environment variables. Each entry follows this format:
VARIABLENAME=VariableValue
like this:
SHELL=/bin/bash
The environment variables list is null-terminated.
IMPORTANT: DO NOT use any argv or envp values directly in calls to system()! This is a huge security hole as malicious users could set environment variables to command-line commands and (potentially) cause massive damage. In general, just don't use system(). There is almost always a better solution implemented through C libraries.
The parameters to main represent the command line parameters provided to the program when it was started. The argc parameter represents the number of command line arguments, and char *argv[] is an array of strings (character pointers) representing the individual arguments provided on the command line.
The main function can have two parameters, argc and argv. argc is an integer (int) parameter, and it is the number of arguments passed to the program.
The program name is always the first argument, so there will be at least one argument to a program and the minimum value of argc will be one. But if a program has itself two arguments the value of argc will be three.
Parameter argv points to a string array and is called the argument vector. It is a one dimensional string array of function arguments.
Lets consider the declaration:
int main (int argc, char *argv[])
In the above declaration, the type of the second parameter named argv is actually a char**. That is, argv is a pointer to a pointer to a char. This is because a char* [] decays to a char** due to type decay. For example, the below given declarations are equivalent:
int main (int argc, char *argv[]); //first declaration
int main (int argc, char **argv); //RE-DECLARATION. Equivalent to the above declaration
In other words, argv is a pointer that points to the first element of an array with elements of type char*. Moreover, each elements argv[i] of the array(with elements of type char*) itself point to a character which is the start of a null terminated character string. That is, each element argv[i] points to the first element of an array with elements of type char(and not const char). A diagram is given for illustration purposes:
As already said in other answers, this form of declaration of main is used when we want to make use of the command line argument(s).
The first parameter is the number of arguments provided and the second parameter is a list of strings representing those arguments.
Both of
int main(int argc, char *argv[]);
int main();
are legal definitions of the entry point for a C or C++ program. Stroustrup: C++ Style and Technique FAQ details some of the variations that are possible or legal for your main function.
In case you learn something from this
#include<iostream>
using namespace std;
int main(int argc, char** argv) {
cout << "This program has " << argc << " arguments:" << endl;
for (int i = 0; i < argc; ++i) {
cout << argv[i] << endl;
}
return 0;
}
This program has 3 arguments. Then the output will be like this.
C:\Users\user\Desktop\hello.exe
hello
people
When using int and char**, the first argument will be the number of commands in by which the programs is called and second one is all those commands
Just to add because someone says there is a third parameter (*envp[]), it's true, there is, but is not POSIX safe, if you want your program to use environment variables you should use extern char environ ;D

how to point to char *argv[] in main?

In c++ I've got a main function with
int argc, char * argv[]
I need to access the data in argv[] (i.e the arguments) in another function.
I am going to declare a global variable, a pointer to the char **argv.
How do I do this?
In C++, usually* the best way to handle argv is this:
int main(int argc, char **argv)
{
std::vector<std::string> args(argv, argv + argc);
}
Now you have args, a correctly constructed std::vector holding each element of argv as a std::string. No ugly C-style const char* in sight.
Then you can just pass this vector or some of its elements as you need.
* It carries the memory & time overhead of dynamically allocating one copy of each argv string. But for the vast majority of programs, command-line handling is not performance-critical and the increased maintainability and robustness is well worth it.
Global variables should be avoided and you should prefer passing arguments. Anyway, you can just use:
char **global_argv = NULL;
int main(int argc, char * argv[]){
...
global_argv = argv;
...
}
A better way would be:
void my_fun1(int argc, char **argv); // Passing all the program arguments
void my_fun2(char *arg); // Passing just the one you need
int main(int argc, char * argv[]){
...
my_fun1(argc, argv);
my_fun2(argv[3]); // Supposing you need the 3rd parameter (fourth on argv)
...
}

Calling c++ from command line with variables specified in CMD or from the system call of another program

Hi I am relatively new to programing.
I want to create a C++ program that when you call it in CMD you can pass it variables.
For example in cmd
Myprograme.exe 11 32 232
So that it uses these values in the calculation.
C++
int main(float A, float B, float C){
float sum= A+B+C;
cout << sum;
return 0;
}
My problem is I don’t know what you would call this process to even Google it.
The standard signature of main is as follows:
int main(int argc, const char **argv)
argc is the number of comman-line arguments that were given to the program (including argument number 0 which is the program's name).
argv is an array of nul-terminated character strings, each of which contains the appropriate command-line argument. argv[argc] is a null pointer.
You can use these to parse the command-line arguments an pass them on to your computation.
For example, if you issue the following on the command line:
myprog.exe a bb c
argc will be 4
argv[0] will be "myprog.exe"
argv[1] will be "a"
argv[2] will be "bb"
argv[3] will be "c"
argv[4] will be the null pointer
The main method can have two arguments :
int main(int argc, char** argv)
{
}
argc is the number of arguments, and argv is an array of char* containing the value of each argument. You can then convert you char* to float as you want. Take care, the first argument is the name of the program itself.
You always have a main function like
int main(int argc, char **argv)
{
}
The first argument is the number of arguments, argv points to argc char*s which are the arguments. This means you get char-arrays instead of floats which is absolutely understandable because you might also write
Myprograme.exe a b cde fg
See Converting char* to float or double for how to convert char* to float.
#danial weaber this is a very good example, but it does not run to the right sum. That is because it is not finding c.
Your example could should be:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
float sum,a,b,c;
sum=atof(argv[0]);
a=atof(argv[1]);
b=atof(argv[2]);
c=atof(argv[3]);
sum=a+b+c;
cout<<sum;
}
Just wanted to point that out, so that when they run it with...
Myprograme.exe 11 32 232 it will then return 275.
Also, some ide's may not run your code correctly.
A lot of times using notepadd++ then compiling your
code in the command line, you may get the correct results. Good Luck
Answer to your first question.
This is called command line argumants.
You can use this keyword to google for it.
This is what you tried to do. First define the main function like this.
int main(int argc, char *argv[]) {
float sum,a,b,c;
a=atof(argv[0]);
b=atof(argv[1]);
c=atof(argv[2]);
sum=a+b+c;
cout<<sum;
}
Now you can pass the arguments Myprograme.exe 11 32 232 it will return 275

What does int argc, char *argv[] mean?

In many C++ IDE's and compilers, when it generates the main function for you, it looks like this:
int main(int argc, char *argv[])
When I code C++ without an IDE, just with a command line compiler, I type:
int main()
without any parameters. What does this mean, and is it vital to my program?
argv and argc are how command line arguments are passed to main() in C and C++.
argc will be the number of strings pointed to by argv. This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.
The variables are named argc (argument count) and argv (argument vector) by convention, but they can be given any valid identifier: int main(int num_args, char** arg_strings) is equally valid.
They can also be omitted entirely, yielding int main(), if you do not intend to process command line arguments.
Try the following program:
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Have " << argc << " arguments:" << std::endl;
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
}
Running it with ./test a1 b2 c3 will output
Have 4 arguments:
./test
a1
b2
c3
argc is the number of arguments being passed into your program from the command line and argv is the array of arguments.
You can loop through the arguments knowing the number of them like:
for(int i = 0; i < argc; i++)
{
// argv[i] is the argument at index i
}
Suppose you run your program thus (using sh syntax):
myprog arg1 arg2 'arg 3'
If you declared your main as int main(int argc, char *argv[]), then (in most environments), your main() will be called as if like:
p = { "myprog", "arg1", "arg2", "arg 3", NULL };
exit(main(4, p));
However, if you declared your main as int main(), it will be called something like
exit(main());
and you don't get the arguments passed.
Two additional things to note:
These are the only two standard-mandated signatures for main. If a particular platform accepts extra arguments or a different return type, then that's an extension and should not be relied upon in a portable program.
*argv[] and **argv are exactly equivalent, so you can write int main(int argc, char *argv[]) as int main(int argc, char **argv).
int main();
This is a simple declaration. It cannot take any command line arguments.
int main(int argc, char* argv[]);
This declaration is used when your program must take command-line arguments. When run like such:
myprogram arg1 arg2 arg3
argc, or Argument Count, will be set to 4 (four arguments), and argv, or Argument Vectors, will be populated with string pointers to "myprogram", "arg1", "arg2", and "arg3". The program invocation (myprogram) is included in the arguments!
Alternatively, you could use:
int main(int argc, char** argv);
This is also valid.
There is another parameter you can add:
int main (int argc, char *argv[], char *envp[])
The envp parameter also contains environment variables. Each entry follows this format:
VARIABLENAME=VariableValue
like this:
SHELL=/bin/bash
The environment variables list is null-terminated.
IMPORTANT: DO NOT use any argv or envp values directly in calls to system()! This is a huge security hole as malicious users could set environment variables to command-line commands and (potentially) cause massive damage. In general, just don't use system(). There is almost always a better solution implemented through C libraries.
The parameters to main represent the command line parameters provided to the program when it was started. The argc parameter represents the number of command line arguments, and char *argv[] is an array of strings (character pointers) representing the individual arguments provided on the command line.
The main function can have two parameters, argc and argv. argc is an integer (int) parameter, and it is the number of arguments passed to the program.
The program name is always the first argument, so there will be at least one argument to a program and the minimum value of argc will be one. But if a program has itself two arguments the value of argc will be three.
Parameter argv points to a string array and is called the argument vector. It is a one dimensional string array of function arguments.
Lets consider the declaration:
int main (int argc, char *argv[])
In the above declaration, the type of the second parameter named argv is actually a char**. That is, argv is a pointer to a pointer to a char. This is because a char* [] decays to a char** due to type decay. For example, the below given declarations are equivalent:
int main (int argc, char *argv[]); //first declaration
int main (int argc, char **argv); //RE-DECLARATION. Equivalent to the above declaration
In other words, argv is a pointer that points to the first element of an array with elements of type char*. Moreover, each elements argv[i] of the array(with elements of type char*) itself point to a character which is the start of a null terminated character string. That is, each element argv[i] points to the first element of an array with elements of type char(and not const char). A diagram is given for illustration purposes:
As already said in other answers, this form of declaration of main is used when we want to make use of the command line argument(s).
The first parameter is the number of arguments provided and the second parameter is a list of strings representing those arguments.
Both of
int main(int argc, char *argv[]);
int main();
are legal definitions of the entry point for a C or C++ program. Stroustrup: C++ Style and Technique FAQ details some of the variations that are possible or legal for your main function.
In case you learn something from this
#include<iostream>
using namespace std;
int main(int argc, char** argv) {
cout << "This program has " << argc << " arguments:" << endl;
for (int i = 0; i < argc; ++i) {
cout << argv[i] << endl;
}
return 0;
}
This program has 3 arguments. Then the output will be like this.
C:\Users\user\Desktop\hello.exe
hello
people
When using int and char**, the first argument will be the number of commands in by which the programs is called and second one is all those commands
Just to add because someone says there is a third parameter (*envp[]), it's true, there is, but is not POSIX safe, if you want your program to use environment variables you should use extern char environ ;D