指针06 - 零基础入门学习C语言46
生活随笔
收集整理的這篇文章主要介紹了
指针06 - 零基础入门学习C语言46
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
第八章:指針06
?讓編程改變世界
Change the world by program
?
字符指針作函數(shù)參數(shù)
出錯(cuò)了:關(guān)于a[]和*a的一些區(qū)別! [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."; //此處應(yīng)該定義為char b[] = "You are a student.";//區(qū)別兩者反匯編代碼并解釋其意義//談?wù)勎鍌€(gè)區(qū)!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] ?優(yōu)化:對(duì) copy_string 函數(shù)還可作簡化
方法一
[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]?獲得所有教學(xué)視頻、課件、源代碼等資源打包?[/buy] [Downlink href='http://kuai.xunlei.com/d/LHAPZBNXYNES']視頻下載[/Downlink]轉(zhuǎn)載于:https://www.cnblogs.com/LoveFishC/archive/2011/02/11/3847022.html
總結(jié)
以上是生活随笔為你收集整理的指针06 - 零基础入门学习C语言46的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL数据库管理常用命令
- 下一篇: TWebBrowser的基本应用