Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > error C2040: 'hFlag' : 'void *' differs in levels of indirection from 'int'

Reply
Thread Tools

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

 
 
Angus
Guest
Posts: n/a
 
      02-12-2007
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?

 
Reply With Quote
 
 
 
 
Clark S. Cox III
Guest
Posts: n/a
 
      02-12-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      02-12-2007
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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-12-2007
jacob navia <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      02-12-2007
Keith Thompson wrote:
> jacob navia <(E-Mail Removed)> 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.

 
Reply With Quote
 
gw7rib@aol.com
Guest
Posts: n/a
 
      02-12-2007
On 12 Feb, 12:11, "Angus" <(E-Mail Removed)> 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.

 
Reply With Quote
 
matevzb
Guest
Posts: n/a
 
      02-12-2007
On Feb 12, 10:49 pm, (E-Mail Removed) wrote:
> On 12 Feb, 12:11, "Angus" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error C2040: 'hFlag' : 'void *' differs in levels of indirection from 'int' Angus C++ 2 02-12-2007 12:31 PM
XSL variable indirection sucheta.phatak@gmail.com XML 2 09-21-2005 05:44 AM
differs in levels of indirection from... blimeyoreilly C++ 10 06-26-2005 02:27 PM
[xinclude] How to use indirection in xinclude like "Catalog" in the entity world ? SL XML 0 02-15-2005 01:58 PM
Problem with "x" differs in levels of indirection from "y" Tobias Erbsland C++ 2 06-18-2004 08:15 AM



Advertisments