This is my first question on the site even though i have been coming here for reference for quite some time now. I understand that argv[0] stores the name of the program and the rest of the commandline arguements are stored in teh remaining argv[k] slots. I also understand that std::cout treats a character pointer like a null terminated string and prints the string out. Below is my program.
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << argv[0] << " ";
cout << argv[1] ;
return 0;
}
According to all the other programs I have seen over my internet search in the issue, this program should printout two strings viz. name of the program and the commandline arguement. The console window shows
0010418c 001048d6
I believe these are the pointers to argv[0] and argv[1] resp.
The only commandline arguement I have is “nanddumpgood.bin” which goes in argv[1] and shows the strings correctly if I mouseover the argv[] arrays while debugging.
Whis is this happening? What am I doing wrong? I understand, arrays decay to pointers in special cases? Is this a case where it doesnt?
That’s mostly correct. It works for
char*, but not other types of characters. Which is exactly the problem. You have a_TCHAR*, which ISchar*on an ANSI build but not on a Unicode build, so instead of getting the special string behavior, you get the default pointer behavior.argvis an array, but neitherargv[0]norargv[1]are arrays, they are both pointers. Decay is not a factor here.The simplest fix is to use
int main(int argc, char* argv[])so that you get non-Unicode strings for the command-line arguments. I’m recommending this, rather than switching towcout, because it’s much more compatible with other code you find on the internet.