Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The app is Windows 7 Enterprise, MFC, Visual Studio 2008, C++, unicode enabled

The argument needed is a single character. VS is configured to provide command line argument, value is "1", argc has incremented from 1 to 2 after adding the command line argument.

char str_number = __argv[ 1 ];

Compile error cannot convert from char* to char, cool, understood

char str_number = *__argv[ 1 ];

Access violation reading location 0x00000004

char str_number = *__argv[ 1,0];

Access violation reading location 0x00000000

What I have tried:

I have tried the examples shown in block: Describe the problem.
Posted
Updated 12-Oct-16 10:25am
Comments
Patrice T 12-Oct-16 12:31pm    
Ans you have a question ?

You first need to check the value of __argc before attempting to refer to any of the __argv elements. Something like:
C++
if (__argc > 1)
    char str_number = *__argv[ 1 ];

Alternatively you can use the CCommandLineInfo Class[^].
 
Share this answer
 
Both
char str_number = *__argv[1];

and
char str_number = __argv[1][0];

should work.

Please check always __argc value before accessing __argv, as pointed out by Richard.
 
Share this answer
 
When your application is unicode enabled you should use __wargv instead of __argv. Try your second example again with that change and you should be in luck.
 
Share this answer
 
Comments
Richard MacCutchan 13-Oct-16 3:09am    
How will that help when OP is trying to access a char?
nv3 13-Oct-16 3:56am    
He is trying to access the first character of the first argument. *__argv[1] should normally do that. But in the case of a unicode application, windows doesn't deliver the argument vector in __argv, but in __wargv instead. That's probably why he got the access violation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900