This page was last updated on March 13th, 2020(UTC) and it is currently August 10th, 2020(UTC).
That means this page is 150 days, 9 minutes and 40 seconds old. Please keep that in mind.

38 - Unions This is going to be a short one. I had a problem, a long time ago, with an API not doing what I wanted. There was a C++ feature that could've saved me a load of trouble, but I had completely forgotten that it exists. Find a way to use this. This is a very useful thing long term. Below, I'm going to make a nifty little tool that will become obsolete relatively soon, but could become useful if applied to the replacement.

#include <stdio.h> //------------------------------------------------------------------------ extern "C" int rand(); //stdlib.h, but djgpp is kinda broken out of the box. extern "C" void srand(unsigned int seed); //Same as above. extern "C" int time(int *junk); //time.h, but likely broken. //------------------------------------------------------------------------ union ipv4{ //Let's escape the type-casting troubles of C++! unsigned char uc[4]; unsigned long int i; }; //------------------------------------------------------------------------ void ipv42name(ipv4 x){ char names[][10] = { //std::string should be more efficient. "Emma", "Olivia", "Sophia", "Ava", "Isabella", "Mia", "Abigail", "Emily", "Charlotte", "Harper", "Madison", "Amelia", "Elizabeth", "Evelyn", "Avery", "Chloe", "Ella", "Grace", "Victoria", "Aubrey", "Scarlett", "Zoey", "Addison", "Lily", "Natalie", "Hannah", "Aria", "Layla", "Brooklyn", "Alexa", "Riley", "Leah", "Audrey", "Momo", "Aiko", "Beli", "Celeste", "Jessie", "Kyanna", "Kyu", "Lola", "Nikki", "Tiffany", "Venus", "Allison", "Savannah", "Samantha", "Nora", "Skylar", "Camila", "Anna", "Paisley", "Ariana", "Claire", "Violet", "Stella", "Sadie", "Mila", "Lucy" //This is enough for a demo. IPv6 needs ALOT more. }; int namenum = sizeof(names)/10; while(x.i != 0){ printf("%s ", names[x.i % namenum]); x.i /= namenum; } printf("\n"); } //------------------------------------------------------------------------ extern "C" void meow(){ srand(time(0)); //Checks for null pointer, which is fine. ipv4 ip; for(int i = 0; i < 4; i++) ip.uc[i] = rand() & 0xFF; printf("0x%08x: %i.%i.%i.%i\n", ip.i, (unsigned int)ip.uc[0], (unsigned int)ip.uc[1], (unsigned int)ip.uc[2], (unsigned int)ip.uc[3]); ipv42name(ip); }

I usually tell people that when I was working on a project, it was 10 times easier and faster to do it in assembly than C++, because C++ wouldn't let me operate IP addresses like ints, which I wanted to do so I could do some math on the IPs to encrypt them or turn them into names like this. I completely forgot about unions. Had I rememberd, it would not have taken me several days to write that program, then give up ('cause type casting is usually trouble when you're not upcasting), and switch to assembly. Don't make my mistake. The usefulness is that, IRL, we remember names better than numbers. If you turn this into std::string on a modern program, slap enough names in there (making sure they don't sound alike) it's perfectly possible to make a single "name" for a website or something with a static IP that's easier to remember than the IP if you don't want to actually register a .com or something like that for it.

Get your own web kitty here!
┬ęCopyright 2010-2020. All rights reserved.