Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hacker's test C functions
#1
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.

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
Reply


Messages In This Thread
Hacker's test C functions - by g4143 - 11-03-2009, 07:38 PM
RE: Hacker's test C functions - by MrD. - 11-03-2009, 07:56 PM
RE: Hacker's test C functions - by g4143 - 11-03-2009, 08:35 PM
RE: Hacker's test C functions - by MrD. - 11-03-2009, 08:57 PM
RE: Hacker's test C functions - by g4143 - 11-03-2009, 09:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Best Functions for File Input/Output? Metapod 3 1,275 06-17-2011, 01:15 PM
Last Post: Mr. House
  Switch Conditionals in Functions not working???? Equinøx 4 868 04-16-2010, 01:16 PM
Last Post: MrD.
  Why are all the functions in programming languages English? nevets04 13 2,875 11-09-2009, 06:07 PM
Last Post: J4P4NM4N

Forum Jump:


Users browsing this thread: 1 Guest(s)