6
clpsplug
332d

Learning C/C++ started to become fun, mainly because I've managed to do seemingly cursed stuff lately.
For example, today, I learned you could totally call a class member function using a type-matching nullptr variable. The 'this' variable is so null that it will crash as soon as the program hits 'this', though, but the control flow does go into the function.
It's not *that* cursed once you learn the behind-the-scene stuff, but that kind of weird s#!+ is funny, even amusing for me, who've mained languages other than C/C++.

Comments
  • 3
    Learning C is a rollercoaster. Much higs and lows. The day I delevered a 'big' application without leaks and stable I felt like a god
  • 5
    // C/C++

    int arr[] = {1,2,3,4,5};

    2[arr] = 42;

    What will the array be filled with?
  • 1
    @Demolishun wtf, no idea
  • 0
    @Demolishun

    for(int val: arr){

    std::cout << val << ",";

    }

    std::cout << std::endl;
  • 3
    @Demolishun

    {1,2,42,4,5}

    Spoilers below
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .

    For those foreign to pointer arithmetic, in C/C++, array indexing is basically address arithmetic (as in, addition) a[2] is the same as 2[a], meaning address of a plus 2 times the size of a.

    Kudos to you. Great brain teasing question for those learning the eldritch mysteries of real languages 🙂
  • 1
    @CoreFusionX I honestly didn't know if that for loop would work until I tried it. I figured it could conclude based upon size of array the loop constraints. Keep that in my back pocket. Wouldn't work with dynamic array I don't think.
  • 1
    @Demolishun

    The iterator notation in c++ works for c arrays (since they are of known size at compile time) and c++ constructs (such as vector) that are aware of iterators.

    It won't work on c++ "dynamic" (as in, runtime sized arrays, since those are pointers, not arrays), and there's a non trivial distinction (unless the compiler can constexpr the size, but I digress).

    The concept of C variable length arrays is not a thing in C++, so can't really consider those.
  • 0
    C++ was one of my first languages. And the more you learn about how C++ works, the more you learn to appreciate higher level languages.

    I love C++ for teaching me stuff and giving me better understanding of programming, but I hate C++ for actual programming.
  • 3
    @Lensflare

    The way I see it?

    Once you know how to drive manual, by all means buy an automatic.

    There's a reason you can't buy a manual if you got your license on automatic.
  • 0
    @CoreFusionX wait, what? Where is this can't get a manual thing? This is wild.
  • 3
    @Demolishun

    In Spain, if you do the exam for your driving license on an automatic transmission car, you can drive AT cars, but not manual ones.

    If you do the exam on a MT car, you can drive both.
  • 2
    @Demolishun As someone that does mainly C programming, i was confused why you ask this, of course it is {1,2,42,4,5}. But then i rember that this doesn't work in other languages.

    In C, n[m] is the same as (*((n)+(m))). So if course n[m] is the same as m[n]
  • 1
    @Lensflare Try writting something in JavaScript and you will prefer C++
  • 1
    @CoreFusionX When you drive an automatic you will have less focus on driving.
  • 1
    @happygimp0 Of course. Anything is better than JS.
Add Comment