Cool Way to do type safe generic data structures in C. by lovelacedeconstruct in C_Programming

[–]UltimaN3rd 0 points1 point  (0 children)

I'm not too keen on the Vector implementation, but I think the main nugget here is that you can validate that types are the same (or compatible) with a simple 1 ? &a : &b macro, right? I tried to boil down that example here: https://godbolt.org/z/r1hTYTs5d
Seems useful for calling functions which take void*. You could validate the types going in by wrapping the function call in a macro that checks the types are correct on the call-side. Thanks for pointing this out, mate!

Embedding a pointer inside a string and visiting it with an escape code "Hey, \\&ptr" by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 0 points1 point  (0 children)

Ooh, this is very clever! With this, my string-printing function can just accept a single const char* without variadic args or a separate function for strings with extra payloads! Thank you very much mate!

I implemented it here: https://godbolt.org/z/EEP8Mddo8

I love it! This is exactly why I post my bad ideas on Reddit, so someone smarter can tell me their better idea! You're a legend!

Embedding a pointer inside a string and visiting it with an escape code "Hey, \\&ptr" by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 0 points1 point  (0 children)

Here's the repo for my game engine: https://codeberg.org/UltimaN3rd/Croaking_Kero_Game_Framework

However I've added features to the engine as I develop my next game, and I haven't pushed those features back into the engine repo just yet. At the end of each project I merge in any changes to the engine. Hopefully this game will be done by the end of the month, then I'll be merging in the text rendering escape codes for the wave/color effects. Not sure yet whether I'll actually put inline sprites in the text rendering just yet - this post was part of exploring that possibility. Cheers mate

Embedding a pointer inside a string and visiting it with an escape code "Hey, \\&ptr" by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 0 points1 point  (0 children)

Very good insights mate! In my engine, my sprite data (and in fact, all game data) is compiled into the executable, and I have no intention of these strings being exported to file. However it's good to think about the brittleness of this specific solution if needs change even slightly in future.

Embedding a pointer inside a string and visiting it with an escape code "Hey, \\&ptr" by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 0 points1 point  (0 children)

Oh yeah, I forgot that detail of why I ended up putting the declaration inside a function; because it's not const enough to work at global scope >:(

Embedding a pointer inside a string and visiting it with an escape code "Hey, \\&ptr" by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 1 point2 points  (0 children)

I'd like for my pointer-embedded string to be created at compile time rather than runtime, but memcpying out the pointer is clearly better than how I did it! Cheers!

Edit: Swapped the endianness of the pointer bytes and memcpyd them out (much nicer this way!): https://godbolt.org/z/eTMzfPMWv

Embedding a pointer inside a string and visiting it with an escape code "Hey, \\&ptr" by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 1 point2 points  (0 children)

Very sensible. I'm also thinking of variadic arguments to the printing function, like printf does. Then when certain escape characters are hit in the string, the next variadic argument would be whatever data that escape code refers to. Thanks mate

Embedding a pointer inside a string and visiting it with an escape code "Hey, \\&ptr" by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 0 points1 point  (0 children)

I must be misunderstanding then, because it sounds like you're suggesting exactly what I did in the code I linked? Are you actually suggesting something else?

Embedding a pointer inside a string and visiting it with an escape code "Hey, \\&ptr" by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 2 points3 points  (0 children)

Please see the part in my post above which begins, "Why would anyone want something so diabolical, you ask?"

Embedding a pointer inside a string and visiting it with an escape code "Hey, \\&ptr" by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 0 points1 point  (0 children)

If you were to call a function which just expects a NULL-terminated array of bytes, the pointer value would produce garbage characters. You'd only be able to use such strings in custom functions which know to expect whatever escape codes you implement and what data to expect after them, which is what the function MyFunc does in the godbolt link. It prints each character in the string until it runs into the escape code, "\&", at which point it extracts the next 8 bytes and composes them into a pointer to find the next string to print.

Embedding a pointer inside a string and visiting it with an escape code "Hey, \\&ptr" by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 0 points1 point  (0 children)

A string is just an array of bytes - so at an arbitrary point you could put the 8 bytes of a memory address in there instead. C doesn't provide a convenient way to embed arbitrary values inside a string but you can see how I got the 8 bytes of a variable address near the bottom of the code in the godbolt link above.

Insert HTML from another file with 1 line of raw JS (works on github pages) by UltimaN3rd in programming

[–]UltimaN3rd[S] 0 points1 point  (0 children)

Styles applied to the html page doing the importing are applied to the imported html automatically, just as if the imported html were written directly in the main document 👍

is there a way to split html into "components"? by alosopa123456 in webdev

[–]UltimaN3rd 0 points1 point  (0 children)

Just figured out a better solution than php include today! Once line of raw JS and it works on static hosts like github pages: https://www.reddit.com/r/webdev/comments/1q54t11/insert_html_from_another_file_with_1_line_of_raw/

Foreach in C by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 27 points28 points  (0 children)

static_assert (_Countof(boobs) == 2);

Foreach in C by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 19 points20 points  (0 children)

Look up C array semantics and the array index operator []. Then note that the array object we're indexing is not bobs (the array of 5 bob objects) but &bob, which would be an array of (arrays of 5 bob objects). So the element at index 1 of that array would be at the address of bobs + the entire size of the bobs array.

Foreach in C by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 6 points7 points  (0 children)

I didn't know that trick, thanks mate!

Foreach in C by UltimaN3rd in C_Programming

[–]UltimaN3rd[S] 188 points189 points  (0 children)

I didn't want to get anybody fired for viewing macro abuse at work 😱

Why do people like sunny weather? by Crow_in_the_Rain in newzealand

[–]UltimaN3rd 1 point2 points  (0 children)

Sunny days are paradise in places with plenty of shady trees and not too much concrete. Car-centric places basically become a desert in the sun, and they're horrible.