0

I would like to know what following syntax does:

func((some_type*) apointer)

Is this a simple type check or does this do something more? Why are there brackets required around the type?

whole example from http://nikhilm.github.com/uvbook/networking.html#tcp:

int main() {
    loop = uv_default_loop();

    uv_tcp_t server;
    uv_tcp_init(loop, &server);

    struct sockaddr_in bind_addr = uv_ip4_addr("0.0.0.0", 7000);
    uv_tcp_bind(&server, bind_addr);

    /* here it is */
    int r = uv_listen((uv_stream_t*) &server, 128, on_new_connection);

    if (r) {
        fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop)));
        return 1;
    }
    return uv_run(loop, UV_RUN_DEFAULT);
}

Regards, Bodo

Update:

Could this work?

typedef struct one_t
{
    int counter;

} one_t;

typedef struct two_t
{
    another_t request;
} two_t;

(one_t*) two_t
Benj
  • 31,668
  • 17
  • 78
  • 127
bodokaiser
  • 15,122
  • 22
  • 97
  • 140

3 Answers3

3

It is known as type cast or type conversion. It is used when you want to cast one of type of data to another type of data.

Jay
  • 24,173
  • 25
  • 93
  • 141
2
(uv_stream_t*)&server

is a cast. It is used here as a polymorphism emulation in C.

uv_tcp_t may be declared like:

typedef struct uv_tcp_t
{
    uv_stream_t base; //base has to be first member for byte reinterpretation to work

    /*...snip...*/

} uv_tcp_t;

This allows uv_listen to operate on uv_tcp_t as if it was an uv_stream_t variable.

It is common, and (AFAIK) perfectly valid C.

milleniumbug
  • 15,379
  • 3
  • 47
  • 71
  • I understand type cast on integers, floats but I do not understand it for structs. Does the reference of server (&server) get mapped to a value of uv_stream_t*? – bodokaiser Mar 27 '13 at 16:29
  • Does this require uv_tcp_t to have the same properties as uv_stream_t? I still do not understand which rules the cast for structs follows – bodokaiser Mar 27 '13 at 16:32
  • I added an example to the starting post – bodokaiser Mar 27 '13 at 16:35
  • @kyogron This type cast is not used on structs, but on pointers. By cast, we tell the compiler "Treat these bytes at the address here as if this was the other type". This means it is extremely unsafe in most cases, but not in this one. Byte reinterpretation here works, because `base` is the first member of uv_tcp_t. – milleniumbug Mar 27 '13 at 16:35
  • I think I get it: uv_tcp_s and uv_stream_s share the first two members. The only difference is that uv_tcp_s has a third member but this is not important because uv_stream_s has no place for it. But this leads me to the question why we actually do this at all I mean when the members are identical the only benefit would be hiding the third member of uv_tcp_t? – bodokaiser Mar 27 '13 at 16:48
  • @kyogron uv_tcp_t has some common properties as uv_stream_t. Here function that expects a uv_stream_t argument can operate on uv_tcp_t. This is called polymorphism and many language newer than C have builtin support for it. – milleniumbug Mar 27 '13 at 16:58
  • but why do they do this? Is this used to hide some special members of uv_tcp_t? – bodokaiser Mar 27 '13 at 17:01
  • 1
    @kyogron You can have multiple related types and use the same function to operate on every type in the same way. – milleniumbug Mar 27 '13 at 17:04
0
(some_type) *apointer

This mean that you cast the apointer content to the some_type type

MOHAMED
  • 41,599
  • 58
  • 163
  • 268