This really isn't a hackers test, its just some really neat code that I was playing with. The whole point of this code is to show the versatility of the void pointer and how you can really be inventive with it....It also demonstrates how easily you can write overly complicated code in C.
Basically the the code just uses a void pointer to hold function addresses and with a few tricks allows us to create a very tricky while statement....
See if you can figure out what's going on...
Copy the code, compile and run. The exe will keep asking for a value until you enter 1234 which is the correct value and then exit.
Note: not sure how a C++ compiler will handle this code...you may have to change some parts to get the old lady language(C++) to accept it...
Basically the the code just uses a void pointer to hold function addresses and with a few tricks allows us to create a very tricky while statement....
See if you can figure out what's going on...
Copy the code, compile and run. The exe will keep asking for a value until you enter 1234 which is the correct value and then exit.
Code:
#include <stdio.h>
#include <stdlib.h>
unsigned int ans = 1234;
void* foundit(void)
{
fprintf(stdout, "you guessed the answer->%d\n", ans);
return (void*)0;
}
void* guessit(void)
{
unsigned int val;
fputs("enter a value->", stdout);
fscanf(stdin, "%d", &val);
if (val != ans)
{
return (void*)&guessit;
}
else
{
return (void*)&foundit;
}
}
int main(int argc, char**argv)
{
void *myans = guessit();
while (myans = ((void* (*)(void))myans)())
{}
return 0;
}
Note: not sure how a C++ compiler will handle this code...you may have to change some parts to get the old lady language(C++) to accept it...
Slackware 13/ArchLinux - C/Assem/Python