error C2040: 'hFlag' : 'void *' differs in levels of indirection from 'int'

Discussion in 'C Programming' started by Angus, Feb 12, 2007.

  1. Angus

    Angus Guest

    I am using a global which is a void*

    I have it defined in one file as:
    void* hFlag;

    and one other header file as:
    extern void* hFlag;

    But I get this compile error:

    error C2040: 'hFlag' : 'void *' differs in levels of indirection from
    'int'

    I can't understand what the problem is. I have another global
    variable which is not void* and that works ok. I assume void* is the
    problem. How can I fix it?
     
    Angus, Feb 12, 2007
    #1
    1. Advertising

  2. Re: error C2040: 'hFlag' : 'void *' differs in levels of indirectionfrom 'int'

    Angus wrote:
    > I am using a global which is a void*
    >
    > I have it defined in one file as:
    > void* hFlag;
    >
    > and one other header file as:
    > extern void* hFlag;
    >
    > But I get this compile error:
    >
    > error C2040: 'hFlag' : 'void *' differs in levels of indirection from
    > 'int'
    >
    > I can't understand what the problem is. I have another global
    > variable which is not void* and that works ok. I assume void* is the
    > problem. How can I fix it?
    >


    Your problem is on line 42. (How can we diagnose such a vague problem?)

    If I had to guess, you're probably trying to add an int to a void*.
    Don't do that.


    --
    Clark S. Cox III
     
    Clark S. Cox III, Feb 12, 2007
    #2
    1. Advertising

  3. Angus

    jacob navia Guest

    Re: error C2040: 'hFlag' : 'void *' differs in levels of indirectionfrom 'int'

    Angus a écrit :
    > I am using a global which is a void*
    >
    > I have it defined in one file as:
    > void* hFlag;
    >
    > and one other header file as:
    > extern void* hFlag;
    >
    > But I get this compile error:
    >
    > error C2040: 'hFlag' : 'void *' differs in levels of indirection from
    > 'int'
    >
    > I can't understand what the problem is. I have another global
    > variable which is not void* and that works ok. I assume void* is the
    > problem. How can I fix it?
    >


    From
    http://msdn2.microsoft.com/en-us/library/kb3dky0e.aspx

    Visual C++ Concepts: Building a C/C++ Program
    Compiler Error C2040

    Error Message
    'operator' : 'identifier1' differs in levels of indirection from
    'identifier2'

    An expression involving the operators has inconsistent levels of
    indirection.

    If both operands are arithmetic or both are nonarithmetic (such as array
    or pointer), they are used without change. If one operand is arithmetic
    and the other is not, the arithmetic operator is converted to the
    nonarithmetic type.

    This can happen when you pass a void * to a function that is expecting
    an integer.
     
    jacob navia, Feb 12, 2007
    #3
  4. jacob navia <> writes:
    > Angus a écrit :
    >> I am using a global which is a void*
    >> I have it defined in one file as:
    >> void* hFlag;
    >> and one other header file as:
    >> extern void* hFlag;
    >> But I get this compile error:
    >> error C2040: 'hFlag' : 'void *' differs in levels of indirection from
    >> 'int'
    >> I can't understand what the problem is. I have another global
    >> variable which is not void* and that works ok. I assume void* is the
    >> problem. How can I fix it?
    >>

    >
    > From
    > http://msdn2.microsoft.com/en-us/library/kb3dky0e.aspx
    >
    > Visual C++ Concepts: Building a C/C++ Program
    > Compiler Error C2040
    >
    > Error Message
    > 'operator' : 'identifier1' differs in levels of indirection from
    > 'identifier2'
    >
    > An expression involving the operators has inconsistent levels of
    > indirection.
    >
    > If both operands are arithmetic or both are nonarithmetic (such as
    > array or pointer), they are used without change. If one operand is
    > arithmetic and the other is not, the arithmetic operator is converted
    > to the nonarithmetic type.
    >
    > This can happen when you pass a void * to a function that is expecting
    > an integer.


    Yes, the web page really says "the arithmetic operator is converted to
    the nonarithmetic type". Presumably that should be "arithmetic
    operand"; converting an operator doesn't make any sense.

    There are no implicit conversions between arithmetic and
    non-arithmetic types (except for the special case of a null pointer
    constant). Possibly the Visual C++ compiler supports such conversions
    as an extension, but it's unwise to depend on them. I seriously doubt
    that the text you quoted is going to help the OP solve his problem.

    Angus: You need to show us some actual code that exhibits the problem.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 12, 2007
    #4
  5. Keith Thompson wrote:
    > jacob navia <> writes:
    > > Angus a écrit :
    > >> I am using a global which is a void*
    > >> I have it defined in one file as:
    > >> void* hFlag;
    > >> and one other header file as:
    > >> extern void* hFlag;
    > >> But I get this compile error:
    > >> error C2040: 'hFlag' : 'void *' differs in levels of indirection from
    > >> 'int'
    > >> I can't understand what the problem is. I have another global
    > >> variable which is not void* and that works ok. I assume void* is the
    > >> problem. How can I fix it?
    > >>

    > >
    > > From
    > > http://msdn2.microsoft.com/en-us/library/kb3dky0e.aspx
    > >
    > > Visual C++ Concepts: Building a C/C++ Program
    > > Compiler Error C2040
    > >
    > > Error Message
    > > 'operator' : 'identifier1' differs in levels of indirection from
    > > 'identifier2'
    > >
    > > An expression involving the operators has inconsistent levels of
    > > indirection.
    > >
    > > If both operands are arithmetic or both are nonarithmetic (such as
    > > array or pointer), they are used without change. If one operand is
    > > arithmetic and the other is not, the arithmetic operator is converted
    > > to the nonarithmetic type.
    > >
    > > This can happen when you pass a void * to a function that is expecting
    > > an integer.

    >
    > Yes, the web page really says "the arithmetic operator is converted to
    > the nonarithmetic type". Presumably that should be "arithmetic
    > operand"; converting an operator doesn't make any sense.
    >
    > There are no implicit conversions between arithmetic and
    > non-arithmetic types (except for the special case of a null pointer
    > constant).


    Pointers can be implicitly converted to bool (_Bool) in C99.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Feb 12, 2007
    #5
  6. Angus

    Guest

    On 12 Feb, 12:11, "Angus" <> wrote:
    > I am using a global which is a void*
    >
    > I have it defined in one file as:
    > void* hFlag;
    >
    > and one other header file as:
    > extern void* hFlag;
    >
    > But I get this compile error:
    >
    > error C2040: 'hFlag' : 'void *' differs in levels of indirection from
    > 'int'
    >
    > I can't understand what the problem is. I have another global
    > variable which is not void* and that works ok. I assume void* is the
    > problem. How can I fix it?


    Is the line that it is objecting to, by any chance, something along
    the lines of hFlag = malloc(something); ? If so, have you included a
    prototype for malloc, eg by including <stdlib.h> ?

    If it's not this then, as others have said, you're going to have to
    post the code...

    Paul.
     
    , Feb 12, 2007
    #6
  7. Angus

    matevzb Guest

    On Feb 12, 10:49 pm, wrote:
    > On 12 Feb, 12:11, "Angus" <> wrote:
    >
    >
    >
    > > I am using a global which is a void*

    >
    > > I have it defined in one file as:
    > > void* hFlag;

    >
    > > and one other header file as:
    > > extern void* hFlag;

    >
    > > But I get this compile error:

    >
    > > error C2040: 'hFlag' : 'void *' differs in levels of indirection from
    > > 'int'

    >
    > > I can't understand what the problem is. I have another global
    > > variable which is not void* and that works ok. I assume void* is the
    > > problem. How can I fix it?

    >
    > Is the line that it is objecting to, by any chance, something along
    > the lines of hFlag = malloc(something); ? If so, have you included a
    > prototype for malloc, eg by including <stdlib.h> ?
    >
    > If it's not this then, as others have said, you're going to have to
    > post the code...

    That was my first thought as well. But he should get two warnings for
    that:
    warning C4013: 'malloc' undefined; assuming extern returning int
    warning C4047: 'initializing' : 'void *' differs in levels of
    indirection from 'int '
    For Jacob's suggestion ("This can happen when you pass a void * to a
    function that is expecting
    an integer.") he should also get two warnings, a bit different though:
    warning C4047: 'function' : 'int ' differs in levels of indirection
    from 'void *'
    warning C4024: 'fcn' : different types for formal and actual
    parameter 1
    Hopefully he'll post the code (and more hopefully, he's not compiling
    it in C++ mode).
    --
    WYCIWYG - what you C is what you get
     
    matevzb, Feb 12, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tobias Erbsland
    Replies:
    2
    Views:
    15,810
    Tobias Erbsland
    Jun 18, 2004
  2. Ollej Reemt
    Replies:
    7
    Views:
    583
    Jack Klein
    Apr 22, 2005
  3. blimeyoreilly
    Replies:
    10
    Views:
    4,572
    Peter Julian
    Jun 26, 2005
  4. Angus
    Replies:
    2
    Views:
    725
    Clark S. Cox III
    Feb 12, 2007
  5. Maxx

    Error C2040

    Maxx, Oct 15, 2009, in forum: C Programming
    Replies:
    6
    Views:
    1,404
    James Kuyper
    Oct 20, 2009
Loading...

Share This Page