Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        去年关闭。
                                                                                            
                
        
#include<stdio.h>
void test(void *arg)
{
    if (arg != NULL)
    {
        int temp = (int *)arg;
        printf("[temp]%d\n", 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]%d\n", k);

    return 0;
}

最佳答案

int k = (int *)arg;

  
  该语句不能与G ++兼容,但是仅GCC警告,为什么?


它不能在C ++中编译,因为int*不能隐式转换为int,因此该语句格式错误。


// int k=*(int *)arg;

  
  为什么这个说法不对?


该语句在C ++中在语法上格式正确。但是arg并不指向int对象(或兼容类型的对象),因此通过指针进行间接访问的行为是不确定的。

关于c++ - GCC/G++对以下方面的不同态度:void *到int ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54115562/

10-11 22:46
查看更多