void * pointer in function
hi
If I create the following function void * fast(void *f) { -||- } ; This way you can pass any type of argument to function and function can return any type . But things get tricky since I have to cast void * type to pointer to the type argument is of , or else I get an error . So how do I check to what type pointer points to without raising exception ? int i=1; void *pi=&i; printf("%d" , *(int *)pi ) ; this prints value of i .Without the cast to (int *)I get an error The following question may seem a bit silly , but I will ask anyways . Why must you cast void * type to whatever type pointer points to ,before you can you can extract the value of a variable pointed to --> ( *( int * )pi ) thank you PS:I could use sizeof( *pi ) ,but that seems a bit unpractical bye |
Check out template functions and classes (the later if you use C++).
Also, sizeof isn't a good way to check what type you are using because the sizeof a type could possibly change with compiler revision, new architecture, ect. |
Another thing, that function could die if you sent it anything other then an integer anyway because the printf statement specifically asks for an integer.
|
hi
My knowledge of C is kinda limited so...what is template function ? Like a function blueprint of some kind ? |
Not quite, in fact now that I am looking through one of my own books I am not sure template functions are possible in C. It has been a long time since I have writen anything other then a template class in C++.
You can, however, get the type of something using the typeof function which works the same way sizeof works. For instance, if you have a pointer called x you want to declare x to the typeof what x points to you could write: type(x*) y; Maybe this will help. |
I dont think C has type() or typeof function
|
by xailer(post 1)
So how do I check to what type pointer points to without raising exception ? you cant, when you cast to a void pointer you are saying the pointer has no type associated with it. so casting to a void pointer is generally quite a bad thing to do. and C has no templates or rtti(run time type identification), that is C++ |
But how do some system function with void * type in their parameter list check what value you passed ?
|
can you give me an example as ive looked for functions that use void pointers but all the ones ive found only care about the pointer, not the value it points to
|
this should work
Code:
enum pt_type |
You can also do it without a struct so you're adding a "header" of sorts to your data:
Code:
enum { INT, CHR, FLT }; |
Dunno if it's what you want, but the OpenGL functions that use void* so that it can accept various types take an extra enum that tells the function what the data type is. (man glTexImage2D for example)
|
hi
Im not shure I completely understand your example itsme86 (Im still kinda week on C ) thank you all for helping me out |
Imagine the ptr as 2 parts. A 1-byte header and a data segment. If you wanted to pass a string to fc() it might look like this in memory:
1 h e l l o \0 The first byte is the type identifier (1 = CHR). An integer might look like this: 0 0 0 8 6 (0 = INT, 0086 = the 32-bit integer fc() should process). So your switch() in the fc() function should test the first byte of the ptr to determine the data type and then have a case for each type. Stick to the struct example shown by luxitan if you want though. The header thing can get messy if you're not 100% about what's going on :) |
hi
this is what I find confusing Quote:
I assume INT is really char and thus '*ptr=INT' saves value of 0 in the first byte . But --> *(int*)(ptr+1)=1; pointer will point one element beyond where it currently does . But how many bytes apart from its current addresswill this address be will depend on the type of element pointer points to . But how did it know that it points to char value ( 1 byte ) when we didn't define pointer as pointer to char ? thank you very much |
All times are GMT -5. The time now is 03:28 PM. |