[NTLUG:Discuss] OT C question

Eric Schnoebelen eric at cirr.com
Sat Feb 18 11:42:35 CST 2006


Spicerun writes:
- Will work (my preferred ways), but
- 
- Aptr + 1;
- Aptr + 7;
- 
- aren't particularly good practice.  Your examples currently work right 
- now because Aptr is a char pointer, whose unit size is 1 byte (8 bits), 
- but if Aptr was an integer pointer whose unit size was 4 bytes (on an 
- x86 processor), you'd be moving the pointer to bytes within your integer 
- and not get the results you're looking for.  Incrementing or 
- Decrementing the pointer will insure that you really are going to the 
- next element of your array rather than a byte inside of your current 
- element.

	Pointer math is guarenteed to adjust the pointer in
sizeof(type) increments, just as pointer increment and pointer
decrement would.  As long as you have the pointer types defined
correctly, pointer math of all forms will work.

--
Eric Schnoebelen		eric at cirr.com		http://www.cirr.com
    "A computer lets you make more mistakes faster than any invention in
    human history - with the possible exceptions of handguns and tequila."
					    - Mitch Ratliffe




More information about the Discuss mailing list