56 Intermediate C Programming
giving the size, by putting nothing between [ and ]. The compiler will automatically cal-
culate the array’s size. Lines 9 to 11 calculate the lengths of the three arrays. So far we
use the same size for different types (int, char, double) but different types actually take
up different amounts of memory, and therefore have different sizes. Thus, these three lines
divide the array sizes by the types’ sizes in order to get the numbers of the elements. Line
12 prints the lengths. As you can see, the program prints the correct lengths. This method
for calculating an array’s size is valid only for constant arrays. If an array is created using
malloc, this method will not work. A later chapter will explain malloc.
Lines 13 to 15 assign the addresses of the first element in each array to the pointers.
These three lines are equivalent to
int * iptr = & arr1 [0];1
char * cptr = & arr2 [0];2
double * dptr = & arr3 [0];3
Do not mix the pointer types. For example, the following statements are wrong:
int * iptr = arr2 ;1
int * iptr = arr1 [1];2
The first is wrong because arr2 is an array of char. The second is wrong because arr1[1]
is an int, and is not an address.
Line 16 prints the values stored at the corresponding addresses. The printed values are
the first elements. Lines 17 to 19 are called pointer arithmetic. Each pointer is advanced by
one. This means specifically, that each pointer now points to the next element of the array.
Line 20 prints the values stored at the corresponding addresses. The printed values are the
second elements. Lines 21 to 23 make each pointer point to the next element. Line 24 prints
the values stored at the corresponding addresses. The printed values are the third elements.
Even though different types have different sizes in memory, the compiler will automatically
move the pointers to correctly point to the next elements.
The sizes of types are not fixed by the C language, and can vary depending on the
computer, operating system, and specific compiler options chosen to compile the code. The
following program prints the sizes of various types:
// ar i thmetic2 . c1
#in clude < stdio .h >2
#in clude < stdlib .h >3
int main ( i n t argc ,char * * argv )4
{5
int arr1 [] = {7 , 2, 5, 3 , 1 , 6, -8 , 16 , 4};6
char arr2 [] = { ’m’ , ’q ’, ’k ’ , ’z ’, ’% ’, ’>’ };7
double arr3 [] = {3.14 , -2.718 , 6.626 , 0 . 5 2 9 } ;8
long i nt addr10 = ( long i nt ) (& arr1 [0]) ;9
long i nt addr11 = ( long i nt ) (& arr1 [1]) ;10
long i nt addr12 = ( long i nt ) (& arr1 [2]) ;11
printf (" %ld , %ld , %ld n" , addr12 , addr11 , addr10 ) ;12
printf (" %ld , %ld n" , addr12 - addr11 , addr11 - addr10 );13
long i nt addr20 = ( long i nt ) (& arr2 [0]) ;14
long i nt addr21 = ( long i nt ) (& arr2 [1]) ;15
long i nt addr22 = ( long i nt ) (& arr2 [2]) ;16
printf (" %ld , %ld , %ld n" , addr22 , addr21 , addr20 ) ;17
printf (" %ld , %ld n" , addr22 - addr21 , addr21 - addr20 );18
long i nt addr30 = ( long i nt ) (& arr3 [0]) ;19
long i nt addr31 = ( long i nt ) (& arr3 [1]) ;20