Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. I love C where types are basically a suggestion rather than a rule

I love C where types are basically a suggestion rather than a rule

Scheduled Pinned Locked Moved The Lounge
data-structures
77 Posts 35 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D David ONeil

    den2k88 wrote:

    C++ is ... hard to read and often very hard to write

    Am I using it wrong, because I've never had these problems?

    “If we get $100,000, we will go to Potato blockchain.” Enable the dream!

    D Offline
    D Offline
    den2k88
    wrote on last edited by
    #45

    Neither did I, until I discovered I "was using it wrong" and had some "true C++ programmer" abusing templates lambda functions like there was no tomorrow.

    GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

    D 1 Reply Last reply
    0
    • D Daniel Pfeffer

      [Program: Shoot in foot](http://www.personal.psu.edu/sxt104/program1.html)

      Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #46

      The C++ one is even more accurate when pointers are involved !

      Do not escape reality : improve reality !

      1 Reply Last reply
      0
      • H honey the codewitch

        The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.

        Real programmers use butterflies

        C Offline
        C Offline
        ColinBurnell
        wrote on last edited by
        #47

        You could always use a union, no need to even cast then (: I wouldn't want to program in C targeting a PC these days; but it is good for 8 & 16 bit embedded work, where you need to get at hardware registers and only have maybe 2K RAM and 16K or 32K ROM.

        H 1 Reply Last reply
        0
        • H honey the codewitch

          The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.

          Real programmers use butterflies

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #48

          If you were using [fwrite](https://en.cppreference.com/w/c/io/fwrite), you wouldn't even need a cast...

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          • H honey the codewitch

            It's good for you - puts hair on your chest.** ** I don't know what to tell if you aren't looking for that. :~

            Real programmers use butterflies

            B Offline
            B Offline
            BryanFazekas
            wrote on last edited by
            #49

            Dang! I chased FAR too many pointers! :laugh:

            D 1 Reply Last reply
            0
            • H honey the codewitch

              I use unions sometimes, but i only needed the cast in two places in the code that inspired this post, and it was all it was ever going to need. :)

              Real programmers use butterflies

              W Offline
              W Offline
              W Balboos GHB
              wrote on last edited by
              #50

              I really liked being able to cast nearly anything to anything. For a cheap and easy (and not too secure) 'encryption' I'd just do something like:

              union {
              char * readable;
              ulong * notSoMuch;
              }

              and then you can trivially make a string unreadable by storing the int array in a text file (lots of options there, too, spaced or other-delimiters? left-zero-filled? Decryption is obvious - and really no overhead as all - I always though of it as the string and its encrypted version coexisting in different planes of their little universe.

              Ravings en masse^

              "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

              "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

              H 1 Reply Last reply
              0
              • W W Balboos GHB

                I really liked being able to cast nearly anything to anything. For a cheap and easy (and not too secure) 'encryption' I'd just do something like:

                union {
                char * readable;
                ulong * notSoMuch;
                }

                and then you can trivially make a string unreadable by storing the int array in a text file (lots of options there, too, spaced or other-delimiters? left-zero-filled? Decryption is obvious - and really no overhead as all - I always though of it as the string and its encrypted version coexisting in different planes of their little universe.

                Ravings en masse^

                "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                H Offline
                H Offline
                honey the codewitch
                wrote on last edited by
                #51

                I use the union technique for that in both C(++) and C#. I don't *always* use it in the former just because i hate declaring new types for one or two lines of code where it will be used. i don't really believe in security by obscurity in most cases, but it may be useful for shrouding source code.

                Real programmers use butterflies

                1 Reply Last reply
                0
                • C ColinBurnell

                  You could always use a union, no need to even cast then (: I wouldn't want to program in C targeting a PC these days; but it is good for 8 & 16 bit embedded work, where you need to get at hardware registers and only have maybe 2K RAM and 16K or 32K ROM.

                  H Offline
                  H Offline
                  honey the codewitch
                  wrote on last edited by
                  #52

                  I'm coding a microcontroller and technically I'm using C++ but barely because I am avoiding templates and the STL. =(

                  Real programmers use butterflies

                  1 Reply Last reply
                  0
                  • F fd9750

                    I have been using C for+/- 32 years and still like it a lot. The fact that yes, you can do anything, does force you to be very careful what you ask for because you will get it. :-\

                    H Offline
                    H Offline
                    honey the codewitch
                    wrote on last edited by
                    #53

                    It's very satisfying to be back to it after being confined to coding in C# so much.

                    Real programmers use butterflies

                    1 Reply Last reply
                    0
                    • H honey the codewitch

                      The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.

                      Real programmers use butterflies

                      U Offline
                      U Offline
                      User 13269747
                      wrote on last edited by
                      #54

                      Quote:

                      I love C where types are basically a suggestion rather than a rule

                      That doesn't sound like C. The only place in C where types are not enforced are in void pointers. Everywhere else you have to literally force the compiler to accept incorrect types. How is this different from other languages?

                      H 1 Reply Last reply
                      0
                      • U User 13269747

                        Quote:

                        I love C where types are basically a suggestion rather than a rule

                        That doesn't sound like C. The only place in C where types are not enforced are in void pointers. Everywhere else you have to literally force the compiler to accept incorrect types. How is this different from other languages?

                        H Offline
                        H Offline
                        honey the codewitch
                        wrote on last edited by
                        #55

                        You misunderstand me. I'm not saying C doesn't enforce types. I'm saying if you want to change the type it's enforcing you just have to give it a little nudge. For example

                        struct S {
                        int x;
                        int y;
                        };

                        ...

                        S s;

                        s.x=1;
                        s.y=1;

                        byte* bp = (byte*)&s;

                        // now i can work on it as a series of bytes

                        Real programmers use butterflies

                        U 1 Reply Last reply
                        0
                        • D den2k88

                          Neither did I, until I discovered I "was using it wrong" and had some "true C++ programmer" abusing templates lambda functions like there was no tomorrow.

                          GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                          D Offline
                          D Offline
                          David ONeil
                          wrote on last edited by
                          #56

                          Ung! That sucks! I will admit that I'm not a great fan of lambda syntax, but I love what it is capable of. Putting them in templates - major Ungh! I would classify that as template metaprogramming, rather than C++, just so I could live in my happy little C++ bubble!

                          “If we get $100,000, we will go to Potato blockchain.” Enable the dream!

                          1 Reply Last reply
                          0
                          • H honey the codewitch

                            The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.

                            Real programmers use butterflies

                            R Offline
                            R Offline
                            Rusty Bullet
                            wrote on last edited by
                            #57

                            It's power and speed meant no extra weight from seat belts allowed. Just like any language, you learned where and how to be careful. "it corners on like it is on rails! (Pretty Woman)

                            1 Reply Last reply
                            0
                            • T The Other John Ingram

                              I had a legacy program that had void****** as part of a sort routine. It worked, i didn't now how. The guy that wrote it was long gone and i spent a afternoon trying to figure out how it worked. In the end i left it alone.

                              E Offline
                              E Offline
                              englebart
                              wrote on last edited by
                              #58

                              That looks like a documentation trick to me. "I don't care what the heck is on the other side of the pointer". What was the max dereference level in the actual usage? A single level?

                              1 Reply Last reply
                              0
                              • K KarstenK

                                some days ago some young collegue casted his enum to mine and I wondered why the program crashed. :~

                                Press F1 for help or google it. Greetings from Germany

                                E Offline
                                E Offline
                                etkid84
                                wrote on last edited by
                                #59

                                pretty sure that's something they teach you not to do in school, funny

                                ~d~

                                1 Reply Last reply
                                0
                                • L Lost User

                                  C# has been improving a lot in that area. You can take a `Span` of various types (some mostly-reasonable restrictions apply) and use `MemoryMarshal.AsBytes` on it to view it has a `Span`, then stash it in a file or whatever. It's nice. Actually paradoxically it's nicer than in C, because in C# you can actually control the layout of fields to whatever degree you need, so you can use this for file headers that have "unaligned" fields. C# is a better low level language than C.

                                  E Offline
                                  E Offline
                                  etkid84
                                  wrote on last edited by
                                  #60

                                  Hopefully people will come to their senses and switch from C# to Java, just saying.

                                  ~d~

                                  L T 2 Replies Last reply
                                  0
                                  • B BryanFazekas

                                    Dang! I chased FAR too many pointers! :laugh:

                                    D Offline
                                    D Offline
                                    davecasdf
                                    wrote on last edited by
                                    #61

                                    And I thought it was age that caused that "southern migration" of my hair. Not much on my head any more, but I think my ankles are getting hairier.

                                    T R 2 Replies Last reply
                                    0
                                    • E etkid84

                                      Hopefully people will come to their senses and switch from C# to Java, just saying.

                                      ~d~

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #62

                                      Sure, and then they can't do any of this. No structs, no spans, no layout control.

                                      1 Reply Last reply
                                      0
                                      • H honey the codewitch

                                        The cast operator is why I can cast my struct directly to an "array" of bytes and stash it in a file. It makes me happy. It's so elegant. So concise. And so dangerous.

                                        Real programmers use butterflies

                                        N Offline
                                        N Offline
                                        NightPen
                                        wrote on last edited by
                                        #63

                                        I miss the days when all code was C. To quote the master: “Moira nodded vigorously. She didn't know what BASIC or COBOL were, except that Wiz said they caused brain damage in those who used them.” ― Rick Cook, The Wizardry Compiled

                                        1 Reply Last reply
                                        0
                                        • D davecasdf

                                          And I thought it was age that caused that "southern migration" of my hair. Not much on my head any more, but I think my ankles are getting hairier.

                                          T Offline
                                          T Offline
                                          trønderen
                                          wrote on last edited by
                                          #64

                                          reminds me of one of my coworkers, said to have 'inverted hair': it was thick and black and ran from his earlobes and downwards.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups