第八章:指针06
让编程改变世界
Change the world by program
字符指针作函数参数
出错了:关于a[]和*a的一些区别! [codesyntax lang="c"]#include <stdio.h> void main() { void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; //此处应该定义为char b[] = "You are a student."; //区别两者反汇编代码并解释其意义 //谈谈五个区! printf("String a = %snString b = %sn", a, b); printf("copy string a to string b:n"); copy_string(a, b); printf("nString a = %snString b = %sn", a, b); } void copy_string( char *from, char *to ) { for( ; *from != '�'; from++,to++) { *to = *from; } *to = '�'; }[/codesyntax]
优化:对 copy_string 函数还可作简化
方法一
[codesyntax lang="c"]#include <stdio.h> void main() { void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %snString b = %sn", a, b); printf("copy string a to string b:n"); copy_string(a, b); printf("nString a = %snString b = %sn", a, b); } void copy_string( char *from, char *to ) { while( (*to = *from) != '�' ) { to++; from++; } }[/codesyntax]
方法二
[codesyntax lang="c"]#include <stdio.h> void main() { void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %snString b = %sn", a, b); printf("copy string a to string b:n"); copy_string(a, b); printf("nString a = %snString b = %sn", a, b); } void copy_string( char *from, char *to ) { while( (*to++ = *from++) != '�') { ; } }[/codesyntax]
方法三
[codesyntax lang="c"]#include <stdio.h> void main() { void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %snString b = %sn", a, b); printf("copy string a to string b:n"); copy_string(a, b); printf("nString a = %snString b = %sn", a, b); } void copy_string( char *from, char *to ) { while( *from != '�' ) { *to++ = *from++; } *to = '�'; }[/codesyntax]
方法四
[codesyntax lang="c"]#include <stdio.h> void main() { void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %snString b = %sn", a, b); printf("copy string a to string b:n"); copy_string(a, b); printf("nString a = %snString b = %sn", a, b); } void copy_string( char *from, char *to ) { while( *to++ = *from++ ) { ; // '�' == 0 } }[/codesyntax]
方法五
[codesyntax lang="c"]#include <stdio.h> void main() { void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %snString b = %sn", a, b); printf("copy string a to string b:n"); copy_string(a, b); printf("nString a = %snString b = %sn", a, b); } void copy_string( char *from, char *to ) { for( ; *to++ = *from++; ) { ; } }[/codesyntax]
方法六
[codesyntax lang="c"]#include <stdio.h> void main() { void copy_string( char *from, char *to ); char *a = "I am a teacher."; char b[] = "You are a student."; printf("String a = %snString b = %sn", a, b); printf("copy string a to string b:n"); copy_string(a, b); printf("nString a = %snString b = %sn", a, b); } void copy_string( char from[], char to[] ) { char *p1, *p2; p1 = from; p2 = to; while( (*p2++ = *p1++) != '�' ) { ; } }[/codesyntax] [buy] 获得所有教学视频、课件、源代码等资源打包 [/buy] [Downlink href='http://kuai.xunlei.com/d/LHAPZBNXYNES']视频下载[/Downlink]