Hi all,

I've decided to take some of my ancient C code and move into the C++ 
world. Per the  recommendations here, I am using VC++ express.

I have found some odd behavior and was hoping someone here could explain it.

Below is my program. I made a few changes which affected the output 
described below the source.

// CppTest3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "stdio.h"

using namespace std;

int main(int argc, _TCHAR* argv[])
{
     cout << "Hello World From About" << endl;

     cout << "The command you typed was \"" << (char *)argv[0] << "\"" 
<< endl;

     if (argc == 1)
     {
         cout << "You have not supplied any command line arguments." << 
endl;
     }
     else if (argc == 2)
     {
         cout << "You have supplied " << argc - 1 << " command line 
argument." << endl;
         cout << "It is \"" << (char *)argv[1] << "\""<< endl;
     }
     else
     {
         cout << "You have supplied " << argc - 1 << " command line 
arguments.";
         cout << " They are: " << endl;
         for (int loopctr = 1; loopctr < argc; loopctr++)
         {
             cout << "(" << loopctr << "): \"" << (char *)argv[loopctr] 
<< "\"" << endl;
         }
     }

     return 0;
}

When I start a new project I get the entry point _tmain() rather than 
main(). This causes the argv[] values to be printed as a single char 
rather than a string of chars, like so:

 > cpptest3 test2 test2 test3
Hello World From About
The command you typed was "c"
You have supplied 3 command line arguments. They are:
(1): "t"
(2): "t"
(3): "t"

Now, when I change it to main(), I get different results. For argv[0], 
"cpptest3", I cast the argv[0] as (char *)argv[0] but left the other 
references uncast.

 > cpptest3 test2 test2 test3
Hello World From About
The command you typed was "cpptest3"
You have supplied 3 command line arguments. They are:
(1): "00345105"
(2): "0034510B"
(3): "00345111"

It appears I am seeing the memory address of the variable, not the contents.

Now, when I cast all the references to argv[], I finally get the results 
I was looking for:

 > cpptest3 test2 test2 test3
Hello World From About
The command you typed was "cpptest3"
You have supplied 3 command line arguments. They are:
(1): "test2"
(2): "test2"
(3): "test3"

So, what is going on with _tmain() and cout?  I'm, currently, clueless.

~Rick


To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/c-prog/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to