Deutsch English Français Italiano |
<v8vlo9$2oc1v$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Thiago Adams <thiago.adams@gmail.com> Newsgroups: comp.lang.c Subject: how cast works? Date: Wed, 7 Aug 2024 08:28:09 -0300 Organization: A noiseless patient Spider Lines: 91 Message-ID: <v8vlo9$2oc1v$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 07 Aug 2024 13:28:10 +0200 (CEST) Injection-Info: dont-email.me; posting-host="18c3ce62a11a8ba38e7d744c933446be"; logging-data="2895935"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18u3pYctpwKGyPPiV0J+hTPaORq0usdgIE=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:EVgPJ/hOCA7qClXjfr2Zg4fUmvM= Content-Language: en-US Bytes: 2946 How cast works? Does it changes the memory? For instance, from "unsigned int" to "signed char". Is it just like discarding bytes or something else? For instance, any 4 bytes type, cast to 2 bytes type is just the lower 2 bytes? [A][B][C][D] -> [A][B] I also would like to understand better signed and unsigned. There is no such think as "signed" or "unsigned" register, right? How about floating point? The motivation problem. I have a union, with unsigned int, unsigned char etc.(all types) I need to execute a cast in runtime (like script). The problem is that this causes an explosion of combinations that I am trying to avoid. --------------------------------------------------------- enum type { TYPE_UNSIGNED_CHAR, TYPE_UNSIGNED_INT, }; struct number { enum type type; union U { unsigned int unsigned_int_value; unsigned char unsigned_char_value; } data; }; struct number cast_to(enum type t, const struct number* n) { struct number r = {0}; r.type = t; r.data = n->data; //maybe fill with zeros the extra bytes? return r; } // All combinations... struct number cast_to2(enum type t, const struct number* n) { struct number r = {0}; r.type = n->type; switch (t) { case TYPE_UNSIGNED_CHAR: switch (n->type) { case TYPE_UNSIGNED_CHAR: r.data.unsigned_char_value = n->data.unsigned_char_value; break; case TYPE_UNSIGNED_INT: r.data.unsigned_char_value = n->data.unsigned_int_value; break; } break; case TYPE_UNSIGNED_INT: switch (t) { case TYPE_UNSIGNED_CHAR: r.data.unsigned_int_value = n->data.unsigned_char_value; break; case TYPE_UNSIGNED_INT: r.data.unsigned_int_value = n->data.unsigned_int_value; break; } break; } return r; }