Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Bedtime Pointer tale

Status
Not open for further replies.

AllVol

New Member
While trying to get my head around the use of C language pointers, I came up with the following bit of drivel.

Hope it entertains, educates, or something.

Enjoy


PHP:
*******************************
*
* Title:  Scratchpad
* Date:   12/4/09
* Author: AllVol
* Processor:     16F690
* Compiler:    HiTech 
*
* Studies the use of pointers and addresses
* 
*********************************/
#include <htc.h>
__CONFIG    (INTIO & WDTDIS & MCLRDIS & UNPROTECT);
 void waste_time(void)
 {
  unsigned short int t;
   for (t=0; t<=20000; t++){
    }
 }
 
 void main()
 {
 
 ANSEL = 0;
 TRISC = 0;
 PORTC = 0;
 
 while (1){   //    A POINTER TO WEALTH
 
 int fabulous_wealth;    //Great riches are locked up somewhere.
 int *trusted_courier;    //I will send a favored servant to bring them
 int my_table;      //to my table, for I do not know where to
 fabulous_wealth = 0x09;    //look for the treasure.
 
 trusted_courier = &fabulous_wealth;  //My man is given the address of the
 my_table = *trusted_courier;  //great fortune and spreads it before me
 PORTC = my_table;     //that I may bask in my wisdom and wealth.
 waste_time();      // forever.....
 PORTC ^=my_table;     //            and......
 waste_time();      //                     ever.
 }       //The  
 }       //   End
 
Good code needs few comments if you use a little imagination naming the variables and functions.
Some code out of the C30 Compiler User’s Guide:
Code:
void __attribute__((boot)) chuck_cookies()
{
int hurl;
int them = 55;
char *where = "far";
splat(where);
/* ... */
}
Much more fun in OOP:
Code:
//
class Animal { }
 class Cat extends Animal { }
  class Lolcat extends Cat { }
  class Longcat extends Cat { }
   class Tacgnol extends Longcat { }
 class Pokemon extends Animal { }
 class Human extends Animal { }
  class Trainer extends Human { }
//
while (Universe.hasMoreSpaceFor(longcat)) {
   longcat.length ++;
}
//
for (Cat lolcat : lolcats) {
   you.look(lolcat);
   you.lol();
}
//
if (lolcat.isHungry()) {
   if (lolcat.canFind(Cheeseburger.class)) {
      Cheeseburger chezburgr = (Cheeseburger) lolcat.find(Cheeseburger.class);
      lolcat.eat(chezburgr);
   } else {
      lolcat.die();
   }
}
//
while (you.hasMoreIdeas()) {
   Idea idea = you.nextIdea();
   if (idea instanceof Lolcat.Idea) {
      Image lolcatimage = you.getSkills(Skill.PHOTOSHOP).makeFromIdea(idea);
      Internet.post(lolcatimage, Internet.Type.IMAGE);
   }
}
//
try {
   Bomb[] bombs = (Bomb[]) you.onHand();
   for (Bomb bomb : bombs) {
      if (you.canFind(Human.class)) {
         Human target = you.find(Human.class);
         you.throwObject(bomb, target);
      }
   }
} catch (ClassCastException e) {
   while (you.canFind(Bomb.class)) {
      you.get(you.find(Bomb.class));
   }
}
//
if (PokemonWorld.inBattle(you)) {
   Pokemon wildPkmn = you.find(Pokemon.class);
   Pokemon yourPkmn = you.getParty().getPokemon("Turtwig").send();
   while (! wildPkmn.getStatus(Pokemon.FAINT)) {
      if (wildPkmn.getHp > 0.5) {
         yourPkmn.useMove("Tackle", wildPkmn);
      } else {
         Object result = ((PokeBall) you.getBag().find("Pok\x00E9 Ball")).throwAt(wildPkmn);
         if (result != null) {
            ((Pokedex) you.getItem("Pok\x00E9dex")).register(wildPkmn);
            break;
         }
      }
   }
}
 
Last edited:
LOL...btw , the colours are awesome :D
Thanks for this :p

Thanks. Just thought I'd share. Can't see much use for pointers, save for the humongeous program with a crock-pot full of variables, however.:p

KChristie: LOL. I'm sure there are more than a few instances of "poetic license" found in programs.;)
 
Can't see much use for pointers
Pointers are very usefull for passing a refernce to a large variable to a function. Function parameters in C are passed on the stack so if your variable is very large it may not even FIT on the remaining stack space. Plus just passing a 16bit pointer is way quicker than passing 32K of data on the stack.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top