views:

55

answers:

4

Hi experts,

Here is the context of the code:

void THREAD_CC server_thread(void *arg)
{
    BIO *client = (BIO *)arg;
        ...
}

Does the expression (BIO *)arg transform the void pointer arg into a pointer that points to BIO? I'm not sure if I got this right or not.

Any help would be much appreciated!

Z.Zen

+1  A: 

Yes. (BIO *) casts the void * pointer (arg) to be of type BIO *

bjg
+1  A: 

It transforms (casts) the void* into a pointer of type BIO*. It does not "point to" BIO.

relet
+6  A: 

It's called a cast; it doesn't transform the pointer, it persuades the compiler to take your word that the incoming pointer (which is untyped) is actually a pointer to BIO, and to treat it as such.

Carl Smotricz
Yes. That's the only right answer possible. Such cast doesn't "transform" anything, it just asks the compiler to shut up.
sharptooth
No, it *does* transform the pointer. If a `void *` pointer and a `BIO *` pointer have different representations, it'll change the former into the latter. It's equivalent to casting `int` to `long` - sure, on *some* systems it might be a no-op, but not in general.
caf
@caf ?? what do you mean? that the "nature" of a pointer can change according to the pointee?
ShinTakezou
@ShinTakezou: Exactly - the size, representation and alignment requirements of *pointer-to-type-A* can differ from that of *pointer-to-type-B*. There are some limitations on this - eg. `void *` and `char *` must be the same, all pointers to `struct` must be the same. This allows, for example, the implementation to tag pointer types with type information that is checked at dereference time.
caf
@caf still I can't understand the point, likely it's my limit. To me, `type *p` is just a way to say that p is a pointer (and this, qualifies it exactly, given a certain machine); to what, it is interesting only to the C language to be able to know which instruction must be produced by `*p = x`, or `p++`, or `p->field`; but the pointer is not changed at all, just the metainfo about it that are needed by the compiler; for `int` e `long` instead, the argument is different, since if they don't match in size, the datum can be really altered
ShinTakezou
ShinTakezou: Although that's how most common machines work today (all pointers look the same underneath), it's not in general required by the C standard (and if the authors of the standard had wanted to make that guarantee, they would have). For example, a machine where `double` must be 8 byte aligned might plausibly use the *lower* `N-3` bits of the pointer value to address `double` s in memory - on such a machine, converting `double *` to and from `void *` would require a shift.
caf
A: 

Your input variable arg is of type void. Typecasting just casts the variable of one type to another. This is useful when you pass pointers as arguments to different functions and typecast them to their original type when dereferencing them.

In the above case your typecasting arg from (void *) type to (BIO *) type. Now you can access the members of the ponter client like you would do to a normal BIO * pointer type.

Praveen S