Sei sulla pagina 1di 2

// Pointers in C language for Newbies by CaptBry.

// actual program and 'article' in Remarks follow - a SIMPLE Demo of Pointers


// C Language demonstration and exploration of Pointers using a Debugger for
// observing the way data is passed and modified in a SIMPLE program.
// A Self-Teaching experiment to learn how Pointers can help
// write subroutines that work for any array passed to them.
// original 14Oct09 MikroC. 4MHz 16F628A, CONFIG = 0x2138, for Debugger ONLY
// Set the Debugger Watch Window to view ALL declared variables:
// if you set the Pointers to display in HEX, they will match the addresses
// shown on the right side of the Watch Window for each array - cool!
// Click BUILD, get "success" then RUN, Debugger, press F8 to step through
// while reading the comments and watching the variables change in Debug.
// Then write your own code to test your new knowledge !
// ENJOY !
//Written on; mikroC, mikroElektronika C compiler
//for Microchip PIC microcontrollers
//Version: 5.0.0.3 all appropriate copyrights to Mike Predko; Genius.
//16F628A @ 4MHz internal OSC, no out put, HS, WDT=OFF
// rewritten 18Dec2010 to clear out some error generating invisible
// characters that creeped into the code. Mostly tabs, and // were guilty.
// END OPENING REMARKS
// DECLARE VARIABLES
char letters[] = "letter";
char message[] = "1234567";
char *pmessage = &message; // doesn't matter; message0 or no dimension
char *pletters = &letters; // next POINTER to an array
char test = 0; // used mostly to show progress in the Debugger
// SUBROUTINES FOLLOW +++++++++++++++++++++++++++++++++++++++++++
// LOADLETTER() Uses a pointer to put one character into an address passed to it
void loadletter (char *addr, char aletter){ // all LOCAL variables
*addr = aletter; //puts ONE letter into an array
// note these are LOCAL variables whose values are passed by the calling stateme
nt.
} // end loadmessage
//LOADARRAY() Uses TWO passed pointers to arrays, and copies #2 into #1
void loadarray (char *addr1, char *addr2){
while (test != 0){//Using NUL terminated ASCII as recommended by Experts
test = *addr2++;//shows each character in 'test' under the Debugger
*addr1++ = test;//copies a single character and THEN increments addr1
}//wend
}//end loadarray // just keeping track of each function bracket

//MY STRING LENGTH() Takes a pointer to your array and RETURNS its length
char mystrlen (char *addr1){ //pass the array's ADDRESS
char i = 0; //use a counter
test = *addr1; //test becomes the first character of the array
//Watch test change in the Debugger as a CHAR
while (test != 0){ //Until you find the NUL (end of array),
++i; //increment the counter
test = *(addr1+i); //Demonstrates math on the Pointer works fine
} //wend //Change the Debugger to show test as DEC
test = i; //Shows the counter's value in Debugger
return(i); //Return the length of the array
}//end mystrlen
void main() {
test = 1; // gives the Debugger a place to show the starting values
// a good programmer would use asm NOP; for less code space ;-)
asm NOP; // insert assembly code No Operation
// message is still "123456" as DEFINEd
*pmessage = 'H'; // message[0] becomes H by using its Pointer
*pmessage++; // pointer increments from 35 to 36; its just a number
*pmessage = 'i'; //message[1] goes to ASCII i
//the array, message, is now Hi3456NUL Where NUL is zero, a terminator
loadletter(++pmessage, '!'); // now message is Hi!456NUL
//loadarray(++pmessage, &'There'); Sorry, we cant pass multiple chars this way
pmessage = pletters; // simple copy of the number. no reference to what it does.
loadletter(pmessage, 'L'); //puts an L into letters0 using re-callable code
test = message[0]; // H from message, not L from letters0. Data intact
!
test = *pmessage; //IS the L from letters0. Each Pointer stays unique
loadletter(++pletters, 'E'); //loads an E into letter[1]
pmessage = pletters; //re-assigning works too
loadletter(++pmessage, 'T'); //load T into letters[2]
pmessage = &message; //set pointer back to the 0 element
pletters = &letters; //of their original arrays
loadarray ( pmessage , pletters ); //effectively a String Copy routine
// most compiliers have a string copy routine built in, but we can't SEE it work
.
pmessage = &message; //redundant, pmessage is still at the first address
test = mystrlen(message); //also works if pmessage is passed
// These codes, message = "1234"; and *message = "1234", don't work.
test=0; // reset; to watch it change in Debug
test = mystrlen(pmessage); //same number comes up, test = 6.
//test is now 6, the length of message, not including the NUL terminator
//note the array in Debug shows TWO zeros,
//yet the string is terminated by the FIRST one. 0 is NULL.
// the defined length of the array just reserves protected space in RAM
while(1 == 1); //Finished; Loop Forever
} //end main()
// I hope this little demonstration program helps people learn Pointers and
// increase the power and versatility of their programming.
// Diferent compiliers use different syntax, so refer to thier unique HELP
// index for examples. The theory is consistent.
// A Pointer is an address of where something is stored in memory. EZ !

Potrebbero piacerti anche