Showing posts with label Pointer Arithmetic. Show all posts
Showing posts with label Pointer Arithmetic. Show all posts

Wednesday, April 3, 2013

Pointer Arithmetic

Pointer arithmetic

Can only use addition and subtraction.

Can use to step through an array - use the increment operator to generate the address of the next element.

2 * sizeof(<type-pointed-to>)

of any array of any type, the following is true:
x[i] is equivalent to *(x+i)

sizeof operator


C does not specify maximum/minimum values or the storage space allocation for any of its types. They are machine dependent. The operator sizeof() is defined which evaluates to the number of bytes allocated for a variable or type. Type char is always 1 byte (which is assumed to be 8 bits) and type wchar_t is provided for Unicode. 

Also, C does not specify the internal representation for data values e.g. negative numbers, real numbers etc The limits and internal representations aremachine dependent . 
Note that the libraries <limits.h> and <float.h> have the details of these.

The type of a sizeof expression is implementation defined, and can be a problem. Since sizeof returns an unsigned integer type, it is safest to cast the return value to ( unsigned long ) and then print it using %lu conversion.