1.字符串与指针
(1) 用字符数组存放一个字符串,然后输出该字符串。
例题一:定义一个字符数组,对它初始化,然后输出该字符串。#includevoid main(){ char string[] = "I love Fishc.com!"; printf("%s\n", string);}
(2) 用字符指针指向一个字符串
例题二:可以不定义字符数组,而定义一个字符指针。用字符指针指向字符串中的字符。#includevoid main(){ char *string = "I love Fishc.com!"; printf("%s\n", string);}
2.字符串中字符的存取方法
对字符串中字符的存取,可以用下标方法,也可以用指针方法!
- 下标法举例:将字符串a复制为字符串b
#include
void main(){ char a[] = "Fishc.com is a good web site!", b[40]; int i; for(i=0; *(a+i) != '\0'; i++) { *(b+i) = *(a+i); } *(b+i) = '\0'; printf("String a is: %s\n", a); printf("String b is: "); for(i=0; b[i] != '\0'; i++) { printf("%c", b[i]); } printf("\n\n");} - 指针方法举例:将字符串a复制为字符串b
#include
void main(){ char a[] = "Fishc.com is a good web site!", b[40], *p1, *p2; int i; p1 = a; p2 = b; for( ; *p1 != '\0'; p1++, p2++) { *p2 = *p1; } *p2 = '\0'; printf("String a is: %s\n", a); printf("String b is: "); for(i=0; b[i] != '\0'; i++) { printf("%c", b[i]); } printf("\n");}
例题:用函数调用实现字符串的复制
- 用字符数组作参数
#include
void main(){ void copy_string(char from[], char to[]); char a[] = "I am a teacher."; char b[] = "You are a student."; printf("string a = %s\nstring b = %s\n", a, b); printf("copy string a to string b:\n "); copy_string(a, b); printf("\nstring a = %s\nstring b = %s\n", a, b);}void copy_string(char from[], char to[]){ int i = 0; while( from[i] != '\0' ) { to[i] = from[i]; i++; } to[i] = '\0';} - 形参用字符指针变量
1 #include
2 3 void main() 4 { 5 void copy_string( char *from, char *to ); 6 7 char *a = "I am a teacher."; 8 char *b = "You are a student.";//此处应该定义为char b[] = "You are a student."; 9 //错误原因是b的内容为常量,*b只是对地址进行偏移,而不能覆盖10 11 printf("String a = %s\nString b = %s\n", a, b);12 printf("copy string a to string b:\n");13 14 copy_string(a, b);15 16 printf("\nString a = %s\nString b = %s\n", a, b);17 }18 19 void copy_string( char *from, char *to )20 {21 for( ; *from != '\0'; from++,to++)22 {23 *to = *from;24 }25 26 *to = '\0';27 }28 29 //丫的,内存出错了,该内存不能被写,为什么? WHY??见第8,9行 具体解释:五大内存分区
在C++中,内存分成5个区,他们分别是堆、栈、自由存储区、全局/静态存储区和常量存储区。 栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清除的变量的存储区。里面的变量通常是局部变量、函数参数等。 堆,就是那些由new分配的内存块,他们的释放编译器不去管,由我们的应用程序去控制,一般一个new就要对应一个delete。如果程序员没有释放掉,那么在程序结束后,操作系统会自动回收。 自由存储区,就是那些由malloc等分配的内存块,他和堆是十分相似的,不过它是用free来结束自己的生命的。 全局/静态存储区,全局变量和静态变量被分配到同一块内存中,在以前的C语言中,全局变量又分为初始化的和未初始化的,在C++里面没有这个区分了,他们共同占用同一块内存区。 常量存储区,这是一块比较特殊的存储区,他们里面存放的是常量,不允许修改(当然,你要通过非正当手段也可以修改,而且方法很多,在一文中,我给出了6种方法)
优化:对 copy_string 函数还可作简化
- 方法一:
#include
void main(){ void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %s\nString b = %s\n", a, b); printf("copy string a to string b:\n"); copy_string(a, b); printf("\nString a = %s\nString b = %s\n", a, b);}void copy_string( char *from, char *to ){ while( (*to = *from) != '\0' ) { to++; from++; }} - 方法二:
#include
void main(){ void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %s\nString b = %s\n", a, b); printf("copy string a to string b:\n"); copy_string(a, b); printf("\nString a = %s\nString b = %s\n", a, b);}void copy_string( char *from, char *to ){ while( (*to++ = *from++) != '\0')//指针运算符优先级高于++ { ; }} - 方法三:
#include
void main(){ void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %s\nString b = %s\n", a, b); printf("copy string a to string b:\n"); copy_string(a, b); printf("\nString a = %s\nString b = %s\n", a, b);}void copy_string( char *from, char *to ){ while( *from != '\0' ) { *to++ = *from++; } *to = '\0';} - 方法四:
#include
void main(){ void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %s\nString b = %s\n", a, b); printf("copy string a to string b:\n"); copy_string(a, b); printf("\nString a = %s\nString b = %s\n", a, b);}void copy_string( char *from, char *to ){ while( *to++ = *from++ ) { ; // '\0' == 0 }} - 方法五:
#include
void main(){ void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %s\nString b = %s\n", a, b); printf("copy string a to string b:\n"); copy_string(a, b); printf("\nString a = %s\nString b = %s\n", a, b);}void copy_string( char *from, char *to ){ for( ; *to++ = *from++; ) { ; }} - 方法六:
#include
void main(){ void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %s\nString b = %s\n", a, b); printf("copy string a to string b:\n"); copy_string(a, b); printf("\nString a = %s\nString b = %s\n", a, b);}void copy_string( char from[], char to[] ){ char *p1, *p2; p1 = from; p2 = to; while( (*p2++ = *p1++) != '\0' ) { ; }}