简体   繁体   English

指针中没有C转换的整数

[英]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" . 我是C语言的新手,我正在创建一个程序,该程序将从命令行读取输入参数,然后进行比较,但是当我尝试编译代码时,我得到了错误"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. 程序参数以字符串形式呈现给main ,它在C中通过( char )指针进行传递。 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() . 后者似乎是您所追求的,为此,您需要使用适当的标准库函数,例如strtol()atoi()甚至sscanf()

There are no automatic conversions between numeric and string types in C. Program arguments are strings. 在C中,数字和字符串类型之间没有自动转换。程序参数是字符串。 Use conversion functions like atoi or strtol 使用转换函数,如atoistrtol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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