Suspicious pointer assignment warning on pointer?

Started by Barry Gordon, June 16, 2015, 11:33:41 AM

Previous topic - Next topic

Barry Gordon

Hi Chaps,

I've spent nearly a day on this and have tried everything.   I have workaround  code (shown below) -but I would actually like to know what I am doing wrong.  So if anyone can shed any light then it would be greatly appreciated please.     

Both ways of doing the job shown below work OK  (i.e. the full code compile and execute) - but one gives a suspicious pointer assignment warning - the other gives no error.  I'm actually beginning to wonder if there is a compiler 'feature' related to user types.

Type MusicKeyboard_DATA
    Note       as Integer
    Velocity  as Integer                                                                         
    End Type
'--
Dim iPT as Any Ptr
Dim ed as MusicKeyboard_DATA Ptr                                                                             

'  Gives Suspicious pointer assignment WARNING
ed = Cast(PLong,GetWindowLong( hWndForm, 0 ) )                  '?Suspicious pointer assignment   why?

'  This combination works
iPT = Cast(PLong,GetWindowLong( hWndForm, 0 ))                  'No warning
ed = iPT

Thanks
Barry

Paul Squires

Hi Barry,

You need to Cast explicitly to the TYPE ptr that you are using. Because "ed" is defined as "MusicKeyboard_DATA Ptr" then you would do as follows:

ed = Cast(MusicKeyboard_DATA Ptr,GetWindowLong( hWndForm, 0 ) )

You used "PLong" and that is defined simply as a LONG PTR which is obviously more generic than the expected "MusicKeyboard_DATA Ptr" hence the suspicious pointer warning.

Using ANY PTR doesn't generate warnings because the compiler believes that the pointer can point to anything.
Paul Squires
PlanetSquires Software

Barry Gordon

Hi Paul,

Sorry not to have replied before - but I've been away on holiday.

Anyway - thanks for this.   Just when I think I have got my head around CAST - along comes a new gotcha.    I had naively assumed that it would only support standard types and it never occurred to me that it would support user types.  So this I guess this brings up a question which I hope is not too stupid - in that what does CAST actually do :-

1) Is CAST just a 'message' to the compiler to confirm that you know what you are doing when the type is ambiguous ?
2) or Is it a real function which generates code and actually does a physical conversion ?
3) or is it a mixture of both?

Thanks
Barry