简体   繁体   English

声明const char *时,“警告:类型名称中的类型默认为'int'”

[英]“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.

相关问题 函数警告C:“警告:&#39;char_array&#39;的类型默认为&#39;int&#39;” - function warnings C: “warning: type of ‘char_array’ defaults to ‘int’” 警告:“ numRest”的类型默认为“ int”(在“ sleep”函数中) - Warning: type of ‘numRest’ defaults to ‘int’ (in function 'sleep') 警告:返回类型默认为“int”[-Wreturn-type] - warning: return type defaults to ‘int’ [-Wreturn-type] 收到警告:变量类型在 float * 类型的函数中默认为 int - Getting warning:type of variable defaults to int in function of type float * 警告:“root”声明中的类型默认为“int”[-Wimplicit-int]| - warning: type defaults to 'int' in declaration of 'root' [-Wimplicit-int]| 将const char *类型转换为int数组 - Convert const char* type to int array “警告:类型默认为‘int’”函数实际返回什么? - what does a "warning: type defaults to ‘int’" function actually return? 警告:'n' 的类型默认为 'int' [-Wimplicit-int] 在 'gcc' 中的警告,但在 'clang' 中没有警告 - warning: type of ‘n’ defaults to ‘int’ [-Wimplicit-int] warning in 'gcc' , but no warning in 'clang' 关于警告:“注意:应为&#39;const int **&#39;,但参数类型为&#39;int **&#39;” - About warning: “note: expected 'const int **' but argument is of type 'int **'” 警告:从不兼容的指针类型 const int (*)[3] 赋值给 int ** - warning: assignment to int ** from incompatible pointer type const int (*)[3]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM