[英]“warning: type defaults to ‘int’ in type name” when declaring const char*
I'm writing a very simple C program, which begins like this: 我正在编写一个非常简单的C程序,它的开始是这样的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main( int argc, char **argv ){
// check that the program has been invoked correctly
if( argc < 3 ){
fprintf( stderr, "Usage: find_char <string> <string>.\n" );
exit( EXIT_FAILURE );
}
char const *source = (const) (*++argv);
For the last line, I get the following warning: 对于最后一行,我得到以下警告:
main.c:17:3: warning: type defaults to 'int' in type name [enabled by default] char const *source = (const) (*++argv);
I tried: 我试过了:
char const *source = NULL;
source = (const) (*++argv);
but gives the same varning for the second line. 但为第二行提供相同的变幅。 What is going on here?
这里发生了什么?
casting to const
amounts to casting to const int
. 强制转换为
const
等于强制转换为const int
。 When type is omitted and only qualified is set, the compiler just assumes int
. 当省略type且仅设置qualified时,编译器仅假定
int
。
Just remove the (const)
cast. 只需删除
(const)
。 You already did the right thing by declaring the pointed values as const
. 通过将指向的值声明为
const
您已经做对了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.