By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,923 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,923 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Feb 12 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
cl*******@gmail.com
Feb 12 '07 #2

P: n/a
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.
Feb 12 '07 #3

P: n/a
jacob navia <ja***@jacob.remcomp.frwrites:
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) ks***@mib.org <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.
Feb 12 '07 #4

P: n/a
Keith Thompson wrote:
jacob navia <ja***@jacob.remcomp.frwrites:
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.

Feb 12 '07 #5

P: n/a
On 12 Feb, 12:11, "Angus" <anguscom...@gmail.comwrote:
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 '07 #6

P: n/a
On Feb 12, 10:49 pm, gw7...@aol.com wrote:
On 12 Feb, 12:11, "Angus" <anguscom...@gmail.comwrote:
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

Feb 12 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.