There is a close relationship between pointers and arrays. Consider this program
fragment:
fragment:
char str[80], *p1;
p1 = str;
Here, p1 has been set to the address of the first array element in str. To access the fifth
element in str, you could write
str[4]
or
*(p1+4)
Both statements will return the fifth element. Remember, arrays start at 0. To access
the fifth element, you must use 4 to index str. You also add 4 to the pointer p1 to
access the fifth element because p1 currently points to the first element of str. (Recall
that an array name without an index returns the starting address of the array, which
is the address of the first element.)
The preceding example can be generalized. In essence, C/C++ provides
two methods of accessing array elements: pointer arithmetic and array indexing.
Although the standard array-indexing notation is sometimes easier to understand,
pointer arithmetic can be faster. Since speed is often a consideration in programming,
C/C++ programmers commonly use pointers to access array elements.
These two versions of putstr()— one with array indexing and one with pointers—
illustrate how you can use pointers in place of array indexing. The putstr() function
writes a string to the standard output device one character at a time.
/* Index s as an array. */
void putstr(char *s)
{
register int t;
for(t=0; s[t]; ++t) putchar(s[t]);
}
/* Access s as a pointer. */
void putstr(char *s)
{
while(*s) putchar(*s++);
}
Most professional C/C++ programmers would find the second version easier to
read and understand. In fact, the pointer version is the way routines of this sort
are commonly written in C/C++.
p1 = str;
Here, p1 has been set to the address of the first array element in str. To access the fifth
element in str, you could write
str[4]
or
*(p1+4)
Both statements will return the fifth element. Remember, arrays start at 0. To access
the fifth element, you must use 4 to index str. You also add 4 to the pointer p1 to
access the fifth element because p1 currently points to the first element of str. (Recall
that an array name without an index returns the starting address of the array, which
is the address of the first element.)
The preceding example can be generalized. In essence, C/C++ provides
two methods of accessing array elements: pointer arithmetic and array indexing.
Although the standard array-indexing notation is sometimes easier to understand,
pointer arithmetic can be faster. Since speed is often a consideration in programming,
C/C++ programmers commonly use pointers to access array elements.
These two versions of putstr()— one with array indexing and one with pointers—
illustrate how you can use pointers in place of array indexing. The putstr() function
writes a string to the standard output device one character at a time.
/* Index s as an array. */
void putstr(char *s)
{
register int t;
for(t=0; s[t]; ++t) putchar(s[t]);
}
/* Access s as a pointer. */
void putstr(char *s)
{
while(*s) putchar(*s++);
}
Most professional C/C++ programmers would find the second version easier to
read and understand. In fact, the pointer version is the way routines of this sort
are commonly written in C/C++.
No comments:
Post a Comment