Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`cout << argv[0]` returning hex value? [duplicate]

Possible Duplicate:
Can not print out the argv[] values using std::cout in VC++

Code:

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    cout << argv[0] << endl;
    system("PAUSE");
    return 0;
}

As you can see, a standard Win32 console application.
The confusing part is, the value of argv[0] is output as 00035B88.
This is being (AFAIK) run with no command-line options so argv[] should not have a value yet. (or is this the problem?)

However, argv[] is declared as a pointer (_TCHAR*) and I heard that cout will print the address of pointers. Is this the case? If so, how can I print/use the value of argv?

like image 230
Nate Koppenhaver Avatar asked Oct 24 '25 19:10

Nate Koppenhaver


2 Answers

_TCHAR is a wide-character type if the executable was built with the Unicode option. cout can't handle wide characters, so instead of turning it into a char * and printing out a string, it (effectively) uses the default void * printer which prints out the address of the string.

Use wcout instead.

like image 109
nneonneo Avatar answered Oct 26 '25 08:10

nneonneo


Hmm. The first array entry in argv (ie. argv[0]) is, if I remember right, the name of the executable. So it might be printing the address of the first character in a c style character array. Try this:

cout << (char *)argv[0] << endl;
like image 24
nemasu Avatar answered Oct 26 '25 10:10

nemasu