Non-portable const in main - c++

Is using the main function in the form:
int main(int argc, const char* argv[])
non-portable and non-standard? The C++ standard says that argv is "pointer to pointer to char". I prefer to use the word const to prevent modification and out-of-bounds in an ​​unknown (to me) area of memory.
Is such a form wrong and non-portable?

Your version is indeed unportable.
The variants that the standard allows are
int main()
int main(int argc, char* argv[])
int main(int argc, char** argv)
The standard allows implementation-defined possibilities, so const might be permitted on some platforms. But if the implementation doesn't define such an alternative then the behaviour of your program is undefined.

Related

C++ How do I save argv to vector as strings

I am trying to save argv into a vector as strings but I keep getting the error: see reference to function template instantiation 'std::vector<_Ty>::vector<_TCHAR*[]>(_Iter,_Iter)' being compiled
I have tried Save argv to vector or string and it does not work
I am using Microsoft Visual Studio 2010.
Here is my code:
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<std::string> allArgs(argv + 1, argv + argc);
return 0;
}
The problem is that std::string doesn't have a constructor for the _TCHAR* type, so you can't generate a vector of strings from an array of _TCHAR*.
Try using as said by #NathanOliver the "normal" version of the main: int main(int argc, char *argv[]).
Or switch to std::wstring.
Note: If not compiling with unicode enabled, _TCHAR* may then be equivalent to char * and the code would not provoke any compiler errors.
Use this:
typedef std::basic_string<TCHAR> tstring;
// Or:
// using tstring = std::basic_string<TCHAR>;
// If you have latest compiler
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<tstring> allArgs(argv + 1, argv + argc);
return 0;
}
And if you would always want to use wide-strings, use this
int wmain(int argc, whar_t* argv[])
{
std::vector<std::wstring> allArgs(argv + 1, argv + argc);
return 0;
}
Or, if you want to have ANSI only (I'd not recommend), just use old style char and main.

Main method is different to those in tutorials

So I am starting with c++ (i am trying to broaden my mind with new languages) but I came across a little issue which confuses me more than what I guess it should...
Using Visual Studio Express 2012, I created a console win32 application in C++ and this is my main method decleration:
// TestApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
however, since I dont know anything about c++, I searched for some tuts online and all of them had there declerations setup in a different manner
#include <iostream>
using namespace std;
int main()
{
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
cin.get();
}
and
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
I tried typing in the "std::cout", but it wouldnt accept it,
could someone just clarify why and the significance of the difference ?
The main method can be define with or without parameters. It all depends upon what you are using your application for.
Take a look at this: https://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fmainf.htm
Also for your program you need to have a return value
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
is (at least I think so) a Windows only library and compiler depending way of declaring the main function.
Definitly not wrong is to declare main like this:
int main(int argc, char const *argv[])
{
//do something
return 0;
}
or like this:
int main()
{
//do something
return 0;
}
This is definitly proper C++ and you can use this universially.
C++ programs may have one of two beginnings:
int main(int argc, char *argv[])
or
int wmain(int argc, wchar_t *argv[])
The first of these gets its arguments (argv) as ANSI charachers, while the second gets "wide" characters -- generally UTF-16 or UTF-32, depending on the platform.
Microsoft define a framework to allow you make code that can compile with either ANSI or wide characters.
int _tmain(int argc, TCHAR *argv[])
Behind the scenes, they have something like this:
#if defined UNICODE
#define _tmain wmain
#define TCHAR wchar_t
#else
#define _tmain main
#define TCHAR char
#endif
They also have helper functions like _tprintf() and _tcscpy().
NOTE: as pointed out by others, the argc and argv params are optional, so you can also have
int main()
and
int wmain()
and (for Microsoft and compatible compilers)
int _tmain()
Also note that while _tmain() is not strictly portable, you can easily create your own #define macros if you want to be portable to other platforms.

Arguments for main in cmdLine

g++ -pthread threads.cpp I'm compiling my program with that command and I don't know how should I specify some arguments given from the keyboard(file names).
int main(int argc,const char argv[]) is the head of my main and f1.txt and f2.txt are the files I want to pass.
./a.out f1.txt f2.txt is what I've typed and the answer was a screen full of unreadable characters.How should I do this?
int main(int argc,const char argv[]) is not correct, this int main(int argc, char** argv) is correct.
Then argv[1] and argv[2] will be your file names.
You should have got a warning from the g++, regarding the second argument in main.

int _tmain(int argc, _TCHAR* argv[]) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between _tmain() and main() in C++?
what is the difference between int _tmain(int argc, _TCHAR* argv[]) and int main(int argc, char** argv)?
I am not clear of the difference.
_tmain is the Microsoft-specific wrapper around "main()". You can use it with either 8-bit ASCII or 16-bit Unicode. Here's the MS documentation for it:
http://msdn.microsoft.com/en-us/library/6wd819wh%28v=vs.80%29.aspx
You can also use _tmain, which is defined in TCHAR.h. _tmain will
resolve to main unless _UNICODE is defined, in which case _tmain will
resolve to wmain.
_tmain is the unicode version of main. I think this is a MS only extension though.

C++ command line strings like Java?

Is there a way to get c++ strings from the commandline like in Java?
public static void main(String[] args)
where args is an array of C++ strings?
Not precisely but you can come close easily.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef vector<string> CommandLineStringArgs;
int main(int argc, char *argv[])
{
CommandLineStringArgs cmdlineStringArgs(&argv[0], &argv[0 + argc]);
for (int i = 0; i < cmdlineStringArgs.size(); ++i)
{
cout << cmdlineStringArgs[i] << endl;
}
return 0;
}
This just uses the overloaded constructor for std::vector that takes a begining/ending iterator pair to copy the command line arguments into the vectors. It is much the same as java from there on.
You can likewise build and object around that vector with utility methods to convert arguments but there is almost no point. Also there are plenty of packages with object that deal with interpreting command line switches and such. ACE, POCO, QT, etc.. all come with such facilities.
You can use a vector to get the char array into strings.
#include <vector>
#include <string>
using namespace std;
int main (int argc, char** argv)
{
vector <string> args (argv, argv + argc);
}
Yes the main function can take 2 arguments
int main(int argc, char* argv[])
Where argc is the number of arguments and argv contains a list of the arguments.
For example if you run your program as:
MyProgram.exe hello
Then
argc = 2
argv[0] = MyProgram.exe
argv[1] = "hello"
Not built in to the language, but its very easy to implement:
#include <vector>
#include <string>
using namespace std;
int main( int argc, char *argv[] ) {
vector <string> args;
for ( int i = 0; i < argc; i++ ) {
args.push_back( argv[i] );
}
// do something with args
}
In C, your main function signature looks like this:
int main(int argc, char** argv).
argc contains the number of arguments passed in by the command line. The name of the executable is in position 0, and adds one to argc.
argv contains an array of strings containing the arguments. Again, position 0 is the name of the executable.
The standard C++ main() signature is
int main(int argc, char* argv[])
where argc denotes the count of commandline arguments and argv[] is an array of primitive C strings holding the commandline arguments. argv[0] is the executable, as invoked from the commandline.
If you are writing a win32 application, you can use GetCommandLineW http://msdn.microsoft.com/en-us/library/ms683156(VS.85).aspx and CommandLineToArgW http://msdn.microsoft.com/en-us/library/bb776391(VS.85).aspx
There is a comment on the CommandLineToArg page about special handling that is needed if your executable has spaces in the path and there are no arguments that you might have to handle.