Wait-a-minute+ADs- Pointers are analgous to using the INDF register on a PIC, no? The INDF register is a pointer to other registers, and can be useful in things like lookup tables, aclulating averages and medians, etc. (Dim light beginning to shine in brain) -- Lawrence Lile ----- Original Message ----- From: +ACI-William Chops Westfield+ACI- +ADw-billw+AEA-CISCO.COM+AD4- To: +ADw-PICLIST+AEA-MITVMA.MIT.EDU+AD4- Sent: Friday, October 20, 2000 7:39 PM Subject: Re: +AFs-PIC+AF0-: CCS - printing strings +AD4- Pointers. Pointers are variables that hold the address of an object in +AD4- memory, rather than the object itself. Pointers are an efficient way of +AD4- handling large objects. Pointers are how you usually deal with dynamically +AD4- allocated objects (however, this was my first exposure to pointers (in PL/1 +AD4- and Pascal), and it took be AGES to +ACI-get it+ACI-, because so few of my programs +AD4- actually used dynamically allocated objects.) +AD4- +AD4- In C, pointers are important and more common because C does not have any +AD4- native +ACI-string+ACI- data type, nor any defined operations for strings (which +AD4- follows, eh?) (However, the compiler has a string CONSTANT type. Go +AD4- figure.) Pointers are a common and preferred way to deal with text +AD4- strings in C. +AD4- +AD4- Now, it's not clear whether pointers make sense (even for strings) on a +AD4- PIC processor. The (low and midrange) PICs don't really have an +AD4- architecture that supports pointers very well (at all?) (arguably, the +AD4- PIC does not have 'memory' of the sort that pointers normally point to +AD4- at all, just +ACI-registers.+ACI-) This means that the compiler has to do some +AD4- fancy footwork to implement pointers (or even to implement strings, come +AD4- to think of it.) Whether it will do a good job is something you have to +AD4- learn by looking at the code that it produces when you try. (or by +AD4- asking people who have done so.) +AD4- +AD4- +AD4- +AD4APg- char strings+AFs-4+AF0AWw-9+AF0- +AD0- +AHsAIg-Pancakes+ACI-,+ACI-Eggs+ACI-,+ACI-Bacon+ACI-,+ACI-Buscuits+ACIAfQA7- +AD4- +AD4APg- +AD4- +AD4APg- The problem with this method is there are nine bytes of storage +AD4- +AD4APg- allocated for +ACI-Eggs+ACI- and +ACI-Bacon+ACI-, but only 5 and 6 bytes, repectivly, +AD4- +AD4APg- of those entries are used and there is no way of recovering the lost bytes. +AD4- +AD4- +AD4- +AD4- const char +ACo-strings+AFsAXQ- +AD0- +AHsAIg-Pancakes+ACI-,+ACI-Eggs+ACI-,+ACI-Bacon+ACI-,+ACI-Biscuits+ACIAfQA7- +AD4- +AD4- +AD4- +AD4- No wasted ROM that way. +AD4- +AD4- Um. The first case +ACI-wastes+ACI- several bytes of each string that is shorter +AD4- than the max. The second case +ACI-wastes+ACI- space to store the pointers to the +AD4- strings. Which is worse will depend on circumstances. +AD4- +AD4- I've been known to do things like: +AD4- +AD4- char strings+AFsAXQ- +AD0- +ACI-Pancakes+AFw-0Eggs+AFw-0Bacon+AFw-0Buscuits+AFw-0Other+AFw-0+ACIAOw- +AD4- +ACM-define STRING+AF8-PAN 0 +AD4- +ACM-define STRING+AF8-EGG 1 +AD4- +ACM-define STRING+AF8-BAC 2 +AD4- +ACM-define STRING+AF8-BUS 3 +AD4- +AD4- /+ACo- +AD4- +ACo- print the Nth (stringID) null-delimitted substring of the big string. +AD4- +ACo- Note that the end of the big sting should have a double null. +AD4- +ACo-/ +AD4- void printstring (int stringID) +AD4- +AHs- +AD4- int i+AD0-0+ADs- +AD4- /+ACo- +AD4- +ACo- Find the (1st character of) the Nth string. +AD4- +ACo-/ +AD4- while (stringID +AD4- 0) +AHs- +AD4- while (strings+AFs-i+AF0- +ACEAPQ- 0) +AHs- /+ACo- Look for separator +ACo-/ +AD4- i+ADs- /+ACo- Next character +ACo-/ +AD4- +AH0- +AD4- i+ADs- /+ACo- Skip the separator +ACo-/ +AD4- if (strings+AFs-i+AF0- +AD0APQ- 0) +AD4- return+ADs- /+ACo- End of big string is an error+ACE- +ACo-/ +AD4- stringID--+ADs- /+ACo- next substring +ACo-/ +AD4- +AH0- +AD4- /+ACo- +AD4- +ACo- Should be pointing to first character of the desired string. +AD4- +ACo- Now output that string... +AD4- +ACo-/ +AD4- while (strings+AFs-i+AF0- +ACEAPQ- 0) +AHs- +AD4- putchar(strings+AFs-i+AF0-)+ADs- +AD4- i+ADs- +AD4- +AH0- +AD4- /+ACo- +AD4- +ACo- Here, we could purchar CRLF if we wanted +AD4- +ACo-/ +AD4- +AH0- +AD4- +AD4- (This code has not been compiled or tested+ACEAIQ-) +AD4- +AD4- This sort of thing can save quite a bit of string space, depending on +AD4- assorted unknowns (number of strings, how they compile down, etc.) (at +AD4- the expense of CPU time, of course. Since strings tend to be human UI +AD4- things, and PICs tend to be pretty zippy compared to people, you +AD4- probably have the CPU time to spare.) +AD4- +AD4- It looks like a lot of code, but a fair number of those while loops +AD4- ought to turn out to be just a couple of PIC instructions each, and +AD4- there's likely to be a pretty good match up with the way constants might +AD4- actually be stored on a PIC (ie as a series of RETLW instructions.) In +AD4- the worst case, you can write printstring() in assembler... +AD4- +AD4- You can (maybe) do tricks with C syntax that may or may not help the +AD4- compiler, like +AD4- while (putchar(strings+AFs-i+AF0-)) +ADs- +AD4- but ... extreme care is advised. (hint: this short version is not quite +AD4- the same as the longer form further up the page. Also, putchar might be +AD4- a macro, and it can be a bad idea to use and similar on macro arguments.) +AD4- +AD4- BillW +AD4- +AD4- -- +AD4- http://www.piclist.com hint: The list server can filter out subtopics +AD4- (like ads or off topics) for you. See http://www.piclist.com/+ACM-topics +AD4- +AD4- +AD4- +AD4- -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu