简体   繁体   中英

integer from pointer without a cast in C

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

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() .

There are no automatic conversions between numeric and string types in C. Program arguments are strings. Use conversion functions like atoi or strtol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM