一本介绍C指针的书--指针和字符串3.2
2012-08-04 00:00
387 查看
Of course, what the above program illustrates is a simple way of copying a string. After
playing with the above until you have a good understanding of what is happening, we can
proceed to creating our own replacement for the standard strcpy() that comes with C. It
might look like:
char *my_strcpy(char *destination, char *source)
{
char *p = destination;
while (*source != '\0')
{
*p++ = *source++;
}
*p = '\0';
return destination;
}
In this case, I have followed the practice used in the standard routine of returning a
pointer to the destination.
Again, the function is designed to accept the values of two character pointers, i.e.
addresses, and thus in the previous program we could write:
int main(void)
{
my_strcpy(strB, strA);
puts(strB);
}
I have deviated slightly from the form used in standard C which would have the
prototype:
char *my_strcpy(char *destination, const char *source);
Here the "const" modifier is used to assure the user that the function will not modify the
contents pointed to by the source pointer. You can prove this by modifying the function
above, and its prototype, to include the "const" modifier as shown. Then, within the
function you can add a statement which attempts to change the contents of that which is
pointed to by source, such as:
*source = 'X';
which would normally change the first character of the string to an X. The const modifier
should cause your compiler to catch this as an error. Try it and see.
Now, let's consider some of the things the above examples have shown us. First off,
consider the fact that *ptr++ is to be interpreted as returning the value pointed to by ptr
and then incrementing the pointer value. This has to do with the precedence of the
operators. Were we to write (*ptr)++ we would increment, not the pointer, but that which
the pointer points to! i.e. if used on the first character of the above example string the 'T'
would be incremented to a 'U'. You can write some simple example code to illustrate this.
Recall again that a string is nothing more than an array of characters, with the last
character being a '\0'. What we have done above is deal with copying an array. It happens
to be an array of characters but the technique could be applied to an array of integers,
doubles, etc. In those cases, however, we would not be dealing with strings and hence the
end of the array would not be marked with a special value like the nul character. We
could implement a version that relied on a special value to identify the end. For example,
we could copy an array of positive integers by marking the end with a negative integer.
On the other hand, it is more usual that when we write a function to copy an array of
items other than strings we pass the function the number of items to be copied as well as
the address of the array, e.g. something like the following prototype might indicate:
void int_copy(int *ptrA, int *ptrB, int nbr);
where nbr is the number of integers to be copied. You might want to play with this idea
and create an array of integers and see if you can write the function int_copy() and make
it work.
This permits using functions to manipulate large arrays. For example, if we have an array
of 5000 integers that we want to manipulate with a function, we need only pass to that
function the address of the array (and any auxiliary information such as nbr above,
depending on what we are doing). The array itself does not get passed, i.e. the whole
array is not copied and put on the stack before calling the function, only its address is
sent.
This is different from passing, say an integer, to a function. When we pass an integer we
make a copy of the integer, i.e. get its value and put it on the stack. Within the function
any manipulation of the value passed can in no way effect the original integer. But, with
arrays and pointers we can pass the address of the variable and hence manipulate the
values of the original variables.
playing with the above until you have a good understanding of what is happening, we can
proceed to creating our own replacement for the standard strcpy() that comes with C. It
might look like:
char *my_strcpy(char *destination, char *source)
{
char *p = destination;
while (*source != '\0')
{
*p++ = *source++;
}
*p = '\0';
return destination;
}
In this case, I have followed the practice used in the standard routine of returning a
pointer to the destination.
Again, the function is designed to accept the values of two character pointers, i.e.
addresses, and thus in the previous program we could write:
int main(void)
{
my_strcpy(strB, strA);
puts(strB);
}
I have deviated slightly from the form used in standard C which would have the
prototype:
char *my_strcpy(char *destination, const char *source);
Here the "const" modifier is used to assure the user that the function will not modify the
contents pointed to by the source pointer. You can prove this by modifying the function
above, and its prototype, to include the "const" modifier as shown. Then, within the
function you can add a statement which attempts to change the contents of that which is
pointed to by source, such as:
*source = 'X';
which would normally change the first character of the string to an X. The const modifier
should cause your compiler to catch this as an error. Try it and see.
Now, let's consider some of the things the above examples have shown us. First off,
consider the fact that *ptr++ is to be interpreted as returning the value pointed to by ptr
and then incrementing the pointer value. This has to do with the precedence of the
operators. Were we to write (*ptr)++ we would increment, not the pointer, but that which
the pointer points to! i.e. if used on the first character of the above example string the 'T'
would be incremented to a 'U'. You can write some simple example code to illustrate this.
Recall again that a string is nothing more than an array of characters, with the last
character being a '\0'. What we have done above is deal with copying an array. It happens
to be an array of characters but the technique could be applied to an array of integers,
doubles, etc. In those cases, however, we would not be dealing with strings and hence the
end of the array would not be marked with a special value like the nul character. We
could implement a version that relied on a special value to identify the end. For example,
we could copy an array of positive integers by marking the end with a negative integer.
On the other hand, it is more usual that when we write a function to copy an array of
items other than strings we pass the function the number of items to be copied as well as
the address of the array, e.g. something like the following prototype might indicate:
void int_copy(int *ptrA, int *ptrB, int nbr);
where nbr is the number of integers to be copied. You might want to play with this idea
and create an array of integers and see if you can write the function int_copy() and make
it work.
This permits using functions to manipulate large arrays. For example, if we have an array
of 5000 integers that we want to manipulate with a function, we need only pass to that
function the address of the array (and any auxiliary information such as nbr above,
depending on what we are doing). The array itself does not get passed, i.e. the whole
array is not copied and put on the stack before calling the function, only its address is
sent.
This is different from passing, say an integer, to a function. When we pass an integer we
make a copy of the integer, i.e. get its value and put it on the stack. Within the function
any manipulation of the value passed can in no way effect the original integer. But, with
arrays and pointers we can pass the address of the variable and hence manipulate the
values of the original variables.
相关文章推荐
- 一本介绍C指针的书--字符串4.1
- 一本介绍C指针的书--指针和字符串3.1
- 一本介绍C指针的书--字符串和字符串数组6.3
- 一本介绍C指针的书--字符串和字符串数组6.2
- 一本介绍C指针的书--字符串和字符串数组6.1
- 【黑马程序员】C语言字符串指针介绍及使用
- 一本介绍C指针的书--指针的类型及数组2.2
- 一本介绍C指针的书--指针的类型及数组2.1
- 一本介绍C指针的书--指针是什么1.3
- 一本介绍C指针的书--指针是什么1.2
- 一本介绍C指针的书--指针是什么1.1
- 一本介绍C指针的书--指针和结构体5.2
- 一本介绍C指针的书--指针和结构体5.1
- 字符串和一级指针
- 字符指针,字符数组,双引号的字符串的区别与联系
- 字符串指针 与 字符串比较的正确方法
- 字符串指针与字符数组的区别
- VC字符串和指针问题
- 经典字符串hash函数介绍及性能比较
- 字符串指针与字符数组的区别