Destructuring in C
h
Pankak

I was wondering if destructuring is possible in C. Here's an example in pseudocode of what I'm thinking of:

Not particularly necessary but I recall finding something about this or something similar, which I can't find now.

@Coder100 @xxpertHacker smert ppl halp

You are viewing a single comment. View All
xxpertHacker

By the power of God Undefined Behavior, I've figured it out:

https://repl.it/@xxpertHacker/destructing-in-C

Both of y'all @Coder100, @MocaCDeveloper dissing C like that, smh, in C, anything is possible.

Pankak

@xxpertHacker wait dafuq that makes no sense

xxpertHacker

@Pankak But it works, doesn't it?

Pankak

@xxpertHacker this is illegal

xxpertHacker

@Pankak Lmao, ofc it is, it's UB; I'm reusing the values that were still left on the stack to automatically fill x and y, but that is exactly how a destructuring operation would be compiled, just push some values to the stack, and use those as the variables, so at least it's performant. (btw, even -O1 will make this fail)

Btw, you probably heard that C# has destructuring, not C.

Pankak

@xxpertHacker I see, so not practical in any sense though

xxpertHacker

@Pankak Nope, not even slightly.

I tried smth like this:

but they got filled with garbage, so, there's no way to do destructuring ¯\_(ツ)_/¯

Pankak

@xxpertHacker wait only one way I could kinda think of it is with a function. You provide with the struct you want to destructure and the number of members, and using your ud behavior destructure it. Maybe also provide it with a callback with the function but this runs into the problem of the function only being able to destructure one struct and not being able to have other arguments (unless we use variadic args and keep the destructured struct last in the params, but still, limitations). Or if you're able to do something like this

UNLESS that works which would still limit it to 1 destructured struct but whatever

Probably easier to just do

foo(bar.1, bar.2)

xxpertHacker

@Pankak Na, doesn't work with params, it has to initialize params from the stack directly for that UB to have worked.

Also, do you want to destructure multiple structs? That sounds weird.

Pankak

@xxpertHacker yeah I have multiple victims

FlaminHotValdez

@xxpertHacker U were dissing C too... hypocrite

xxpertHacker

@FlaminHotValdez Lmao, I was dissing C's preprocessor, not the language itself.

Pankak

@xxpertHacker no you were dissing macros, theres nothing wrong with the preprocessor. I agree macros are evil beings which shall not be touched, but its kind of like that one neighbor who snipes dogs when they poop in his yard and is a pure evil life form which you shall never be associated with, but eventually you're gonna have to ask him to borrow his sniper to assassinate your ex girlfriend

xxpertHacker

@Pankak Lmao, just don't get the gf and you're safe all around.
Better yet, move out of the neighborhood, so you don't have to deal with either, your gf, or the evil neighbor. (equivalent to not using C at all)

There are legitimate uses for the preprocessor: handling different versions of the language and checking compiler flags.

Btw, what's so bad about sniping dogs?

FlaminHotValdez

@xxpertHacker lol. how many languages have you dissed? it's at least 1

Pankak

@xxpertHacker

Btw, what's so bad about sniping dogs?

Sniping dogs is animal abuse, use something less painful like a landmine, bomb, 10 petawatt laser, or use concentrated plutonium vapor to provide the creature with a less agonizing death, it's a win-win

xxpertHacker

@FlaminHotValdez Hmmm... a whole lot :)
I've dissed the following:

  • HTML (in favor of XHTML)
  • XHTML (in favor of Lisp syntax)
  • C
  • C++
  • JavaScript
  • TypeScript
  • Python
  • CSS
  • Node.js (doesn't count)
  • Java
  • C#
  • Lisp
  • Go (I think?)
  • PHP
  • English (the human language)
  • ...(Click to view ∞ more)
xxpertHacker

@Pankak

Sniping dogs is animal abuse, use something less painful like a landmine, bomb, 10 petawatt laser, or use concentrated plutonium vapor to provide the creature with a less agonizing death, it's a win-win

You have to offset the price with the value, a bullet isn't too expensive, but getting an explosive is expensive.
It also takes time for the black-market smuggler to ship a landmine.
In contrast, the dog isn't worth much, so the dog doesn't pay for itself.

Also, what if I want to eat the animal afterward? Its corpse needs to stay intact.

FlaminHotValdez

@xxpertHacker lol, remember when you said C++ was useless and I proved u wrong

xxpertHacker

@FlaminHotValdez No? When? Can you link this discussion?

FlaminHotValdez

@xxpertHacker in the why does no one use java? i told u they used c++ in competitive programming

xxpertHacker

@FlaminHotValdez Oh yeah, I disagreed though.

FlaminHotValdez

@xxpertHacker lmao yea, but the thing is they only allow C, C++, Java, Python, basically the biggest langs in USACO, Codeforces, all the big contests. among those C++ has the fastest execution time and is therefore favored.

xxpertHacker

@FlaminHotValdez You said this before, ik

Pankak

@xxpertHacker

tell me you haven't lost braincells reading his answers, I sometimes question why I'm even on a website full of 11 year olds

xxpertHacker

@Pankak

tell me you haven't lost braincells reading his answers,

I've lost just about all of 'em reading Ryan's answers, most are not even remotely helpful.
The only thing that's worse than the answer, is the fact that people mark them as "correct."
Some of those suggestions aren't even languages.

I sometimes question why I'm even on a website full of 11 year olds

I'm usually on GitHub, or ... anywhere else, I just use Repl to run code. (a desktop PC would be 10x better)
That, and I check out whenever someone pings me for C/C++.

(for lulz; reference to Ryan)

Please mark this as correct by clicking the checkmark on the left side

Pankak

@xxpertHacker

I'm usually on GitHub

The only thing that's worse than people marking them as "correct" is being a programmer and spending most of your time on GitHub. You're sure hella addicted to version control.

xxpertHacker

@Pankak Na, not really.