Reputation: 6350
I am new in C i am create a program which will read input parameter from command line and then it will do the comparison, but when i am trying to compile the code i am getting error "warning: initialization makes integer from pointer without a cast"
.
I found many example on stack overflow for this error but i am unable to relate the issue in my code . Below is the sample code :
void validateGuess(int guess) {
if (guess < 555) {
printf("Your guess is too low.");
} else if (guess > 555) {
printf("Your guess is too high.");
} else {
printf("Correct. You guessed it!");
}
}
int main(int argc, int** argv) {
int arg1 = argv[0];
validateGuess(arg1);
}
Below error i am getting :
test.c: In function ‘main’:
test.c:14: warning: initialization makes integer from pointer without a cast
Upvotes: 0
Views: 236
Reputation: 181714
The program arguments are presented to main
as strings, which in C are conveyed via (char
) pointers. Although it is allowed to convert pointers to integers, the language requires such conversions to be performed explicitly, via a cast operator. Some compilers will perform such conversions implicitly, however, as an extension. Yours is doing this, but presenting a warning because that might not be what you really wanted.
And indeed it is not what you really wanted. Converting the pointer to an integer is not at all the same thing as parsing the content of the string to which it points. The latter appears to be what you're after, and for that you need to engage an appropriate standard library function, such as strtol()
, atoi()
, or even sscanf()
.
Upvotes: 1