473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

(float *()) means???

Hi all:
I'm confused with the expression "(float *())".
Book says that this is a cast. But, I have no idea of this expr.
why could this expr ignore the variable???

Thanx!!!
Nov 14 '05 #1
6 2276
Dave win wrote:
Hi all:
I'm confused with the expression "(float *())".
Book says that this is a cast. But, I have no idea of this expr.
why could this expr ignore the variable???


Casts are not expressions, they are methods of converting
scalar values and pointers from one type to another. e.g.

int x = (int) 3.14159;
void (*boo)(void) = (void (*)(void)) &strcmp;

Casts are generally dangerous as they override type safety.
Hence, casts should generally be avoided.

The cast you quote is actually a constraint violation as it
has function type, (not function pointer type,) so if the
book literaly cites that as an example, you should either
ditch the book altogether, or advise the authors that there
is an error in their work.

--
Peter

Nov 14 '05 #2
Dave win wrote:
Hi all:
I'm confused with the expression "(float *())".
Book says that this is a cast. But, I have no idea of this expr.
why could this expr ignore the variable???

Thanx!!!


I believe it is typically written like this:

(float (*)())x

This will cast x to be a pointer to a function taking no arguments and
returning a float. It is equivilent to this:

typedef float (*pf)(void)
(pf)x
- Shawn

Nov 14 '05 #3
Dave win wrote:
Hi all:
I'm confused with the expression "(float *())".
Book says that this is a cast. But, I have no idea of this expr.
why could this expr ignore the variable???

Thanx!!!


(float (*)(void)) casts an address, such as 0x0040010A, to a function
pointer of type float with no arguments. Such as:
#define myFunction 0x0040010A

float (*ptrFunc)(void) = NULL;

int main() {
ptrFunc = (float (*)(void))myFunction;
float a = ptrFun();
return 0;
}
--
Ken Human
Nov 14 '05 #4
On Tue, 15 Feb 2005 21:50:48 -0500, Shawn <am*******@sneakemail.com>
wrote:
Dave win wrote:
Hi all:
I'm confused with the expression "(float *())".
Book says that this is a cast. But, I have no idea of this expr.
why could this expr ignore the variable???

Thanx!!!
I believe it is typically written like this:

(float (*)())x

This will cast x to be a pointer to a function taking no arguments and
returning a float. It is equivilent to this:


The arguments the function takes, if any, are unspecified. You must
use void to specify there are no arguments.

typedef float (*pf)(void)
(pf)x
- Shawn


<<Remove the del for email>>
Nov 14 '05 #5
Dave win wrote:
I'm confused with the expression "(float *())".
Book says that this is a cast. But, I have no idea of this expr.
why could this expr ignore the variable???


Sometimes you want to create something that doesn't know what it is
dealing with. You can just hand it some random data in void pointers
(that is, a pointer that officially just points to "some unknown
something, maybe") plus some extra info which tells you precisely what
that void* is supposed to point to.
For example in the below example, I keep an array of void pointers to
all of my math functions. Then I have an array of desired operations
which, in conjunction with the type of my datatype, I use to access the
array of math functions.

#define TYPE_INT 0
#define TYPE_FLOAT 1
struct genericNumber { /*a generic variable type that can pretend to be
any kind of number*/
int type; /*what type it should be interpretted as*/
int intVal;
float floatVal;
};

#define OP_ADD 0 /*possible operations*/
#define OP_SUB 1
int addInts(int a, int b) { /*a function for each operation and each
possible type of number*/
return a + b;
}
float addFloats(float a, float b) {
return a + b;
}
int subInts(int a, int b) {
return a - b;
}
float subFloats(float a, float b) {
return a - b;
}

void* funcs[] = {addInts, addFloats, subInts, subFloats}; /*keep them
in an array so that we can perform operations based on some sort of
script instead of hard coded functionality*/

int opCount = 5;
int ops[] = {OP_ADD, OP_ADD, OP_SUB, OP_ADD, OP_SUB}; /*operations we
want to perform and the order we want to perform them*/

int main(void) {
int L;
struct genericNumber a;
struct genericNumber b;

a.type = TYPE_INT;
a.intVal = 5;

b.type = TYPE_FLOAT;
b.floatVal = 3.0f;

/*run all operations, indexing into our function array based on
operation and variable type. Righthand parameter will be cast to be the
same as lefthand. The result of the operation will be written back into
the variable that was the lefthand parameter (a)*/
for (L = 0; L < opCount; L++) {
if (a.type == TYPE_INT) {
if (b.type == TYPE_INT) {
a.intVal = ((int (*)(int, int))funcs[ops[L] <<
1])(a.intVal, b.intVal); /*cast our void* to a function of the correct
type. We are assuming that all of our math is correct and that our
functions are similarly all in the right order in our function array*/
}
else if (b.type == TYPE_FLOAT) {
int temp = (int)b.floatVal;
a.intVal = ((int (*)(int, int))funcs[ops[L] <<
1])(a.intVal, temp);
}
}
else if (a.type == TYPE_FLOAT) {
if (b.type == TYPE_INT) {
float temp = (float)b.floatVal;
a.floatVal = ((float (*)(float, float))funcs[(ops[L] << 1])
+ 1)(a.floatVal, temp);
}
else if (b.type == TYPE_FLOAT) {
a.floatVal = ((float (*)(float, float))funcs[(ops[L] <<
1]) + 1)(a.floatVal, b.floatVal);
}
}
}

return 0;
}

This is the kind of thing I can imagine you might do if you were
building the engine for a scripting language that used generic variable
types. If you just assume that for every operation there will only be
two inputs and one output, then you would parse the script code and
create arrays of generic variables, lefthand and righthand parameters,
what variable to assign the result to, and what operation to perform.
Voila, Perl.

* note that I just wrote that code in there and have not tested, so no
guarantees that it works correctly.

-Chris

Nov 14 '05 #6
Ken Human <ke******@comcast.net> writes:
Dave win wrote:
Hi all:
I'm confused with the expression "(float *())".
Book says that this is a cast. But, I have no idea of this expr.
why could this expr ignore the variable???

Thanx!!!


(float (*)(void)) casts an address, such as 0x0040010A, to a function
pointer of type float with no arguments. Such as:
#define myFunction 0x0040010A

float (*ptrFunc)(void) = NULL;

int main() {
ptrFunc = (float (*)(void))myFunction;
float a = ptrFun();
return 0;
}


0x0040010A is an integer, not an address (though it may happen to be
the representation of some address).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.