Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
//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 !