> From: Lawrence Lile [mailto:llile@toastmaster.com] ... > Here's #1 and #2 of 999 ways to NOT manipulate strings in a PIC '873: > > 1. Turn on low voltage programming and forget you did this > 2. Execute the following bug-riddled code: > > char array[15]; > char *pString; > > pString = array[0]; // [sic] #1 should be pString = &array[0] > > void makestring(char c){ > *pString++ = c; > } > > printf(makestring, "Good Morning, Vietnam!"); // [sic] #2 > buffer overrun > > puts(array); > ... > I'd like to have a way to check for bounds inside the > makestring() function, pass it the beginning and ending > address of the array, then generate an error or do nothing if > pString points outside of the array in question. I don't > think you can have a function inside printf() that has > arguments, though. At least I haven't gotten anything to work yet. Since you're already dependent on variables external to makestring(), you might want to keep another variable indicating how many characters you've already written. Here's a quick and dirty sample of untested code: #define STR_LEN 15 char array[STR_LEN]; int iString = 0; void makestring(char c) { if (iString < (STR_LEN - 1)) /* array is 0 .. STR_LEN - 1 */ { array[iString] = c; i++; } else { /* do error stuff */ } } This keeps you from referencing incorrect addresses as well as overflowing any allocated space. -Mike -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body