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

A

Angus

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?
 
C

Clark S. Cox III

Angus said:
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.
 
J

jacob navia

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.
 
K

Keith Thompson

jacob navia said:
Angus a écrit :

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.
 
G

Guest

Keith said:
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.
 
G

gw7rib

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.
 
M

matevzb

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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top