[NTLUG:Discuss] OT C question

Greg Edwards greg at nas-inet.com
Sat Feb 18 12:06:59 CST 2006


Spicerun wrote:
> If I may (not that I'm some great expert or anything), can I recommend 
> that Integers not be directly added to a pointer, but that the pointer 
> is incremented or decremented to thr right position?
> 
> IE-
> 
> Aptr++;
> Aptr += 7;
> Aptr[7];
> 
> 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 

> 
> --Spicerun
> 

Not a problem.  Pointer sizing is a preprocessor issue same as constant 
type casting.  The compiler will take care of the type casting and sizing 
of structure (or type) pointer and increment the physical pointer that 
many bytes.  A ++ptr is the same thing as ptr=(ptr+1) or ptr=&ptr[1]. 
Thats why pointer arithmetic is portable between architectures.

OBTW, type size is determined by compiler not processor.  The default 
integer is 2 bytes signed (-32768 to 32767) and long is 4 bytes.  The 
reality is that most compilers support 4 byte integers, but they don't 
have to.

-- 
Greg Edwards
Galactic Outlaw - The Ultimate Cyber Space Adventure
http://GalacticOutlaw.com




More information about the Discuss mailing list