GCC/G++ 的不同态度:void* 到 int
GCC/G++ different attitudes for: void* to int
#include<stdio.h>
void test(void *arg)
{
if (arg != NULL)
{
int temp = (int *)arg;
printf("[temp]%dn", temp);
}
}
int main()
{
int a = 3;
int *b = &a;
int t = b;
test((void *)11);
void * arg = (void *)22;
int k = (int *)arg;//this statement can not compaile with G++,But GCC only Warning,why?(evernt use cpp rewrite it again)
// int k=*(int *)arg;// why this statement not right?
printf("[k]%dn", k);
return 0;
}
int k = (int *)arg;
这个说法不能和G++兼容,但是GCC只是警告,为什么呢?
它不会在 C++ 中编译,因为int*
不能隐式转换为 int
,因此语句格式不正确。
// int k=*(int *)arg;
为什么这种说法不对?
该语句在C++的语法上格式良好。但是arg
不指向int
对象(或兼容类型的对象),因此通过指针进行间接的行为是不确定的。
相关文章:
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 不能使这种类型的"void(C::* volatile)(int) const "在参考手册C++示
- 如何使模板函数像 R f<void(int)>(args...)
- template<class T, int N> void h(T const(&)[N]); as friend function
- 函数 LNK2019 "int __cdecl __scrt_common_main_seh(void)" 中引用的未解析外部符号主错误 (?__scrt_common_main_seh@@YAHXZ
- 从不兼容的类型 'void (Button::*)(int)' 分配给'void (*)(int)'
- 错误:无法将"打印"从"void (*)(int)"转换为"std::function<void()>
- 标准的大小::函数<void(int&)>类型
- std::bind1st是否可以用于将void(*)(void*,int)转换为void(*int)
- "void (*)(int)"与"void (^)(int)"相同吗?
- boost::function<void (int)> 类型兼容性,是怎么做到的?
- void(int) 和 void (*)(int) 之间的区别
- 在Linux上编译简单信号代码时,从void*到void (*) int的无效转换错误
- 错误:将'const Tarih'作为'void(int)' 'this'参数传递丢弃限定符|
- void(int) 和 void(*)(int) 有什么区别?
- 警告(不合时宜):将 void(*)(int) 分配给 extern "C" void(*)(int)
- 为什么 C++11 不支持 'std::function<void(int, ...)>'?
- 为什么在void/int声明后使用const