Re: void * pointer convert problem.




Eric J.Hu wrote:
> Christopher,
>
> Thansk for your quick answer.
> If need do casting back, that's not what I want. Following is my program. I
> just want to ignore which structure it refer to. Please see line 342.
>
> 314 u_long hbHash( upm_histogram_bin_p hPtr, u_long key,
> histogram_bin_type_tag hbt)
> 315 {
> 316 u_long i;
> 317 void* bPtr;
> 323 if( hbt == PROC_TIMING_BIN)
> 324 {
> 325 bPtr = (proc_timing_bin_p)(&hPtr->ptBin[0]);
> 326 }
> 327 else if(hbt == EXEC_TIMING_BIN)
> 328 {
> 329 bPtr = (exec_timing_bin_p)(&hPtr->etBin[0]);
> 330 }
> 340 for (i =0 ; i< MAX_HIST_BIN_SIZE; i++)
> 341 {
> 342 if(key < bPtr->key)
> 343 return i;
> 344 else
> 345 continue;
> 346 bPtr ++;
> 347 }
> 351 return INVALID;
> 352 }
>
> Thanks,
> Eric

Hi Eric,

Conversations make more sense that way.
Folks here don't like top posting, please post after the thing being
replied to.
(above two lines flipped on purpose)

Anyway, I think you aren't clear on the issue with the way
you are trying to use void *.

You (or at least the compiler) can't know without casting
or assigning to something with a non-void type what object type a
pointer to void actually refers to. You are saying on line 342 that
bPtr, a pointer to void, is actually a pointer to some struct that has
a
key field. But the compiler has no way to know what type of structure
that may be (or in particular, what the offset of the key field is
within that
structure).

-David

.



Relevant Pages

  • Re: is const necessary in eg int compar(const void *, const void *)
    ... void *' arguments to assign to a temporary pointer of the appropriate ... avoid casting. ... except that a pointer may be assigned to a _Bool. ... int by_age(const void *vsl, const void *vsr) ...
    (comp.lang.c)
  • Re: Common misconceptions about C (C95)
    ... of the macro is to bind the allocation to the casting of the void ...  The casting of the void pointer is still superfluous. ... Smart programmers don't quote sigs. ...
    (comp.lang.c)
  • Re: Common misconceptions about C (C95)
    ... of the macro is to bind the allocation to the casting of the void ...  The casting of the void pointer is still superfluous. ...
    (comp.lang.c)
  • Re: Dynamically allocating memory for handles
    ... If you can initialize a variable with a valid and correct value, ... (possibly, pointer to void) ... a pointer to void. ... about casting an integer to a pointer: ...
    (alt.comp.lang.learn.c-cpp)
  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)