search

Home  >  Q&A  >  body text

c++ - 如何解决long int * 转换成volatile int *类型的编译错误.

PHP中文网PHP中文网2807 days ago721

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 15:34:37

    No error will be reported in C, but there will be a warning:

    >>> gcc -o a a.c
    a.c: 在函数‘main’中:
    a.c:8:7: 警告:从不兼容的指针类型赋值 [-Wincompatible-pointer-types]
         a = b;
           ^
    a.c:5:19: 警告:变量‘a’被设定但未被使用 [-Wunused-but-set-variable]
         volatile int *a = NULL;
                       ^
    >>> cp a.c a.cpp
    >>> g++ -o a a.cpp
    a.cpp: 在函数‘int main()’中:
    a.cpp:8:9: 错误:不能在赋值时将‘long int*’转换为‘volatile int*’
         a = b;
             ^

    The reason for the warning is clear, int and long are incompatible. An error will be reported in C++ because C++'s type system is more strict.

    Please label the question correctly to avoid attracting non-professional people and wasting both parties’ time.

    reply
    0
  • PHPz

    PHPz2017-04-17 15:34:37

    has nothing to do with volatile. int and long themselves are of different types (can have different lengths). Of course you will get an error if you pass it off like this. First check whether there is any problem with the logic of your code, and then use type conversion with caution.

    reply
    0
  • Cancelreply