void *ptr;
what is the correct way to cast "ptr" in the code above to be a pointer to a 3 element array of function pointers with a return value of int and a single parameter of a pointer to char.
void *ptr;
what is the correct way to cast "ptr" in the code above to be a pointer to a 3 element array of function pointers with a return value of int and a single parameter of a pointer to char.
I don't believe function pointers exist in C. You may want to check the C++ board.
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
No as per my knowledge there are function pointers in C. it is defined like int (*func)(int, int)
> I don't believe function pointers exist in C.
Of course they do - just look at qsort()
> what is the correct way to cast
It's not guaranteed that you can cast a data pointer (even void*) to and from a function pointer.
But the first step would be to create a typedef for the function pointer itself, just to save an awful lot of parens in the result.
typedef int (*fnp)(char*);
A simple cast would then be
fnp myfun = (fnp)ptr;
I'm sure you can work out the array bit from there.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Hey Saleem that was pretty nice trick. but i'm not so good with "typedef" and function pointers please show the answer for the above question in simple step so that i can learn from u....
That was the example for you to learn from.
But since you spent an absolute maximum of 11 minutes thinking about it (probably a lot less), I think I'll pass.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
ok. so for the above question is the following cast proper
Code:(int ( (*)[3]) (char *) ) ptr
No. You cannot assign function pointers to void pointers. No matter how you cast them.
Quzah.
Hope is the first step on the road to disappointment.
void pointers are always defined for flexibility we can cast void pointers to anything....
No you can't. I'm right. You're wrong. Search the forum, I've proven this point before.
Quzah.
Hope is the first step on the road to disappointment.
> void pointers are always defined for flexibilityOriginally Posted by c99
Yes, but not all pointers are the same size - the C standard doesn't say they have to be, and some very common architectures have had different sized pointers. void* only guarantees to be able to represent conversions to/from data pointers.
Eg. In DOS 'medium' memory model, all function pointers would be 32-bit and void* would only be 16-bit.
If you can do it, then put it in the "works for me" column, but one day you may have a surprise.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Probably the solution is getting deviated..... can below be a solution for the problem
Code:(int ( (*)[3]) (char *) ) ptr