Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine the Original Type of Pointer???????

Status
Not open for further replies.

maltobji

Programmer
Feb 13, 2001
31
JO
Dear ALL,
HOW inside a function i could know the previous type of the pointer if i received it as Void pointer(void *)??????
Example:
if a pointer(lets say int *) is Casted to Void pointer (void *).. and this void pointer is then sent to that function....(Assuming i don't know previously the type)...

Thanx in advace for any help...
Mohammed Al-Tobji
Electrical Engineer
 
put it the function a second int/char*... type parameter in thich you will indicate the type. In function you will use usual switch/case or if()else...if.... Ion Filipski
1c.bmp


filipski@excite.com
 
thanx for ur time ...
but the problem is ... that i don't know the source of the pointer...it isn't me who wrote the main....
Any way... what i'm trying to do is like what sizeof() function do.... do u know how this function calculte the size of this huge number of data types..????(even arrays or user-defined data types)

thanx again for ur concern
s Mohammed Al-Tobji
Electrical Engineer
 
once you have a void*, you can't get sizeof *someVoidVariable. Ion Filipski
1c.bmp


filipski@excite.com
 
it seems that my Sentence was miss leading ... i did not say that i want to use sizeof to determine the size of a sizeof *someVoidVariable... what i'm saying that how sizeof knows this large number of sizes .. i'm asking if all are passed as void * or not ......

any way ... i was asked a question from a friend ... how would u determine the type of a variable (between int or float) ... if u don't know the real data type before...???

any help will be greatful .... thanx alot in advance
Mohammed Al-Tobji
Electrical Engineer
 
A function that receives a pointer whose type is specified
i.e.

fxn(int* ptr) or fxn(float* ptr) can be determined just by the function itself. BUT... it may require you to declare each function for each type of pointer to be passed. However, if the function is

fxn(void* ptr) and we want to know how to cast this back there needs to be something else to let us know how to cast it.

As Ion suggested above, there would need to be another parameter.

Matt
 
Hi maltobji...

Apart from what Matt and Ion said above, I would like to add this...

In response to your "what i'm trying to do is like what sizeof() function do...."

sizeof is actually an operator not a function. :) Ankan.

Please do correct me if I am wrong. s-)
 
... and, continuing what ankan said, sizeof knows the size of a variable by its type.
That's why u can't say
Code:
void *ptr;
long l = sizeof(ptr);
You should get an error sounding like Size of type is unknown or zero...
The compilers KNOW the size of a type, once one declared it, so sizeof uses THAT information to retrieve the size of a pointer. The size of a variable (not a pointer!) is given by the size of its type.

NOTE THAT THE SIZE OF A POINTER is ALWAYS 4 BYTES, at least on the systems i worked with, and that is because a pointer simply holds a memory address, which can be stored in a 4 byte number. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
maybe if the pointer is defined as pointing always to a structure what contains its type in some format(int or string usualy), use
((some_struct*)someVoidPtr)->typeOrSize...
perhaps it is a pseudocode, not a predefined function/operator calling or, member getting, see C object oriented programming. Ion Filipski
1c.bmp


filipski@excite.com
 
By the way I've just written faq205-1078 on Object Oriented programming in C. Ion Filipski
1c.bmp


filipski@excite.com
 
Thanx alot guys for your valuable time .... Mohammed Al-Tobji
Electrical Engineer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top