C++ int main(int argc, char* argv[]) arguments usage problem - c++-cli

The common language runtime support is set to (/clr). When I try to write(display) the argument argv[1] nothing happens. Any help please. Compilation warning states- forcing value to bool 'true' or 'false' (performance warning)
int main(int argc, char* argv[])
{
Console::WriteLine(argv[1]);
}

The problem is that there is no Console::WriteLine function taking a char*. You should convert it to a String object.
Regarding the warning message and the output, it's because since the compiler can't find an exact match for the WriteLine call, it will, if possible, pick another function. In this case it picks the one taking a Boolean argument, because pointers can implicitly be converted to bool which in turn can be converted to Boolean. The output is "true" because any non-null pointer is considered true in a pointer-to-bool conversion.

Related

Using arg[v] as int? - Objective C

I'm trying to pass arguments into and integer variable at the start of my program in xcode using Objective C. So far most methods I've found have just given me a variety of error messages or crashed my VM.
int numcols, numrows;
int main(int argc, const char * argv[]) {
#autoreleasepool {
if (argc != 3){
NSLog(#"%2s", "Wrong number of arguments");
exit(1);
}
//numrows = (int)(argv[1] - '0');
//numcols = (int)(argv[2] - '0'); These lines cause a crash
numrows = 3;
numcols = 4;
I want numrows to take the first argument and numcols to take the second. I went into the settings in xcode to change the starting arguments to 3 and 4, but if I check them or print them they come out as random numbers.
argv is an array of C strings. You cannot just cast a string to an integer, you need to parse the string to produce the integer value it represents.
You could write code to do this yourself, but fortunately there are library functions which handle this, for example see strtol which will parse a string and produce a long.
HTH
First of all your question is not clear, how much i understand i can say. As argc means argument count and argv means argument value. If you want to cast char to int. try this
[[NSString stringWithFormat:#"%s", argv[2]] intValue];

Why does the main function in objective-c preserve a C-style?

This could sound a bit weird.
In main.m, it is written as such:
int main(int argc, char * argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([RTAppDelegate class]));
}
}
which is a C-style main function (though with typical [class method] function call). But if we look at syntax of objective-C, one might think of something like this:
+(int)main:(int)argc :(char*) argv[] //I don't really know if (char*) argv[] will be legit in obj-c
{
// DO SOMETHING
return 0;
}
So I'm getting confused about the language itself. Does objective-c simply extends C-syntax? Or is it an independent language itself?
Objective-C is a superset of C. As such, you can (or at least, should be able to) compile any C program through an Objective-C compiler.
Knowing this, the ANSI C standard states that the correct declaration of main is either int main(int argc, char** argv) or int main(void).
http://c-faq.com/ansi/maindecl.html

Scanf countdown

I am making a scanf countdown via objective-C, so the program will count down from what ever number you input. However, there's an annoying semantic error in the code saying:Data argument not used by format string. Also the program doesn't countdown, it just displays the output as zero once I input a number.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
int x,number;
NSLog(#"please enter a number:");
scanf("i", &number);
for (x = number; x>=0; x--)
NSLog(#"%i",x);
}
return 0;
}
You need to pass %i, not i in the format string of scanf.
When you pass i, the format string has zero format specifiers, leading to the semantic analyzer to produce a warning. That's also the reason why nothing gets entered into your number variable, so the countdown does not happen either.

Ordering of Preprocessor statement gives me warning?

I have a code as following
ApplicationSetting.h
FOUNDATION_EXPORT BOOL *const TEST_MODE;
ApplicationSetting.m
#ifdef DEBUG
BOOL *const TEST_MODE = YES;
#else
BOOL *const TEST_MODE = NO;
#endif
The above .m file's code gives me this warning
Incompatible integer to pointer conversion initializing 'BOOL *const'
(aka 'signed char *const') with an expression of type 'signed char';
But, if I change it to be come like this
#ifdef DEBUG
BOOL *const TEST_MODE = NO;
#else
BOOL *const TEST_MODE = YES;
#endif
It works just fine without any warning.
Do you have any idea how could this happens?
You really meant to write a value:
FOUNDATION_EXPORT const BOOL TEST_MODE;
…BOOL is not an objc object, it is a signed char.
as far as the error, the compiler complains because you are assigning numeric values to the pointer value -- where only 0 (aka NULL) is acceptable to the compiler, and any other number (YES is 1) will produce the error/warning.
P.S. Just use bool.
Just to explain in more detail:
Incompatible integer to pointer conversion …
You tried to convert an integer value—a number—to a pointer. This can be done, but it's usually a bad idea and consequently requires a high level of explicitness. It's hard to do by accident (nowadays/on this compiler), and there are reasons for that.
… initializing 'BOOL *const' (aka 'signed char *const') …
This is the type of variable you declared. As this part of the message explains, BOOL is also known as signed char (i.e., the one is typedef'd to the other).
char is the smallest of the integer types in C, so you've declared this variable to hold a pointer to an integer.
… with an expression of type 'signed char';
The expression in this case is the initializer from your declaration. It's the part that you changed between the two versions of the declaration: YES in one case, NO in the other.
The Objective-C headers define NO as 0 and YES as 1, both cast to BOOL (which, as noted above, is defined as signed char).
So:
Your initializer is a BOOL value (as justin rightly pointed out, BOOL with no *), which is an integer.
Your variable holds a BOOL *—a pointer.
The compiler will not let this fly without you being very explicit that this is something you mean to do.
Even if you did convince the compiler to go along with this, it would not be correct code.
As justin already established, you should leave out the *. This will declare the variable as holding a BOOL value, not a pointer.
I also second his suggestion of using bool instead. Unlike BOOL, a bool can never be any value except true (1) or false (0), unless you try very hard.

simple code with EXC_BAD_ACCESS

I am new to the objective c and i write the code according to a reference book.
but something went wrong and I don't know why.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
if (argc==1){
NSLog(#"you need to provide a file name");
return (1);
}
FILE *wordFile = fopen(argv[1], "r");
char word[100];
while(fgets(word , 100, wordFile)){
word[strlen(word)-1] = '\0';
NSLog(#"the length of the %s is %lu", word, strlen(word));
}
fclose(wordFile);
return 0;
}
the tool indicates that the while part went wrong, EXC_BAD_ACCESS.
Any idea?
It compiles and runs fine on my machine. But imagine you have an empty line in your file. Then strlen(word) will return zero. Hence word[strlen(word)-1] = '\0'; will try to set some memory which might not be valid since word[-1] might not be a valid memory cell, or a memory cell that you can legally access.
Oh, and by the way, it has nothing to do with objective-c. This is mostly (but for the NSLog call) pure ansi C.