4

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?

Community
  • 1
  • 1
Nate Koppenhaver
  • 1,676
  • 3
  • 21
  • 31

3 Answers3

6

_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.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 1
    oh, so `_T` is for wide characters in Windows.. Oh God, whyyy.... –  Sep 26 '12 at 04:11
  • 1
    `_T` means "text", and its interpretation depends on the definition of the `_UNICODE` macro (if defined, it's `wchar_t`, else it is `char`). This also determines whether you get the 'A' (narrow) or 'W' (wide) versions of certain functions. See http://msdn.microsoft.com/en-us/library/cc194799.aspx. – nneonneo Sep 26 '12 at 04:14
1

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;
nemasu
  • 426
  • 2
  • 10
  • I can't comment on other peoples posts yet, so I'll put it here: TCHAR will change whether the build is a unicode build or not. But it sounds like wcout will fix the problem. – nemasu Sep 26 '12 at 04:10
  • Rather, more specifically, `_TCHAR`'s definition is dependent on `_UNICODE`. – nneonneo Sep 26 '12 at 04:16
0

In your project settings->General. Go to "Character encoding" and select "multibyte character set". And change your _tmain() to main(). Now it will print your characters.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
user739711
  • 1,842
  • 1
  • 25
  • 30