- 论坛徽章:
- 0
|
string copy problem
1, Garbage book indeed !! throw it away!!
2, char *a = "I am a teacher.";
char a[] = "I am a teacher.";
are totally different.
3, be ware if the len of src string is larger than the len of dest string.
4, my code
- #include <stdio.h>;
- #include <stdlib.h>;
- #include <string.h>;
- void copy_string(char *, char *);
- int main(void) {
- char a[] = "I am a teacher.";
- char b[] = "You are a student.";
- printf("\nstring a = %s\nstring b = %s\n", a, b);
- // copy_string(a, b);
- copy_string(b, a);
- printf("\nstring a = %s\nstring b = %s\n", a, b);
- return 0;
- }
- void copy_string(char *from, char *to) {
- int lenf = strlen(from);
- int lent = strlen(to);
- if(lenf>; lent){
- char * to= (char *)malloc(lenf);
- }
- for(; *from != '\0'; from++, to++) {
- *to = *from;
- // putchar(*to);
- }
- *to = '\0';
- }
复制代码 |
|