C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
FIGURE 25.1 The array name is a pointer to the first value in the array.

Because the array name is a pointer (that can’t be changed), you can print the first value in the array
like this:


Click here to view code image


printf("The first value is %d.\n", vals[0]);

But more important for this chapter, you can print the first array value like this, too:


Click here to view code image


printf("The first value is %d.\n", *vals);

As you’ll see in a moment, this is also equivalent and accesses vals[0]:


Click here to view code image


printf("The first value is %d.\n", *(vals+0));

Warning

The fact that an array is a fixed constant pointer is why you can’t put just an array name
on the left side of an equals sign. You can’t change a constant. (Remember, though, that
C relaxes this rule only when you first define the array because C has yet to fix the
array at a specific address.)

Getting Down in the List


Because an array name is nothing more than a pointer to the first value in the array, if you want the
second value, you only have to add 1 to the array name and dereference that location. This set of
printf() lines


Click here to view code image

Free download pdf