因為部落格不能使用小於符號,所以小於符號我改用全形的方式設計。
請看以下的程式:
#include <iostream>
using namespace std;
int main(){
char *s3="Welcome";
char *s5="Good morning";
s3=s5;
cout<<"s3="<<s3<<endl;
cout<<"s5="<<s5<<endl;
s5="123";
cout<<"s3="<<s3<<endl;
system("PAUSE");
return 0;
}
我們一行一行看...
char *s3="Welcome";
char *s5="Good morning";
s3與s5 沒有配置記憶體空間,"Welcome"與"Good morning"
算是編譯器在編譯期間就決定字串的位址,並沒有真正一塊記憶體
存放 "Welcome" & "Good morning"。
s3=s5;
上面這一行的意思是s3將指標指向的位址指向s5現在指向的位址
,也就是s3指標會指向 "Good morning"這一個字串位址。
s5="123";
上面這一行因為 "123"沒有記憶體空間,所以把 s5 改成 "123",
而s3 還是不變,也就是說s5指標指向 "123"這個字串的記憶體位置。
所以並沒有修改原有字串的內容。
指標要用 malloc 先配置記憶體,才可以在改變存在 malloc 產生的記憶體裡面的字串。
若是要進行字串複製動作,c與c++內不是使用=來指定,而是使用 strcpy()。
例如:
#include <iostream>
using namespace std;
int main(){
char s8[]="Good morning";
cout<<"s8="<<s8<<endl;
strcpy(s8,"this is a testing");
cout<<"s8="<<s8<<endl;
system("PAUSE");
return 0;
}
不過下面的複製字串寫法會有問題的,
因為 s3是指標但是沒有指向任何合法
可提供寫入的記憶體內。
#include <iostream>
using namespace std;
int main(){
char *s3="Good morning";
cout<<"s3="<<s3<<endl;
strcpy(s3,"this is a testing");
cout<<"s3="<<s3<<endl;
system("PAUSE");
return 0;
}
所以常用的方式就是將指標指向合法可用的記憶體區段內再來存取,
使用 malloc() 配置記憶體提供使用:
區段內都可以解決。
#include <iostream>
using namespace std;
int main(){
char *s3=(char *) malloc(sizeof(char) * 100);
strcpy(s3,"this is a testing");
cout<<"s3="<<s3<<endl;
system("PAUSE");
return 0;
}
指向其他可以寫入的記憶體:
#include <iostream>
using namespace std;
int main(){
char *s3;
char s8[]="Good morning";
s3="Welcome";
cout<<"s3="<<s3<<endl;
cout<<"s8="<<s8<<endl;
s3 = s8;
strcpy(s3,"this is a testing");
cout<<"s3="<<s3<<endl;
cout<<"s8="<<s8<<endl;
system("PAUSE");
return 0;
}
綜合以上的說明,如果是兩個指標,可以用以下的方修改字串資料:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
char *s3,*s5;
s3=(char *)malloc(10 * sizeof(char));
strcpy(s3,"Welcome");
s5=(char *)malloc(15 * sizeof(char));
strcpy(s5,"Good morning");
cout<<"first s3="<<s3<<endl;
cout<<"first s5="<<s5<<endl;
s3=s5;
cout<<"second s3="<<s3<<endl;
cout<<"second s5="<<s5<<endl;
printf("second s3=%s\n",s3);
printf("second s5=%s\n",s5);
strcpy(s5,"123");
cout<<"third s3="<<s3<<endl;
cout<<"third s5="<<s5<<endl;
system("pause");
return 0;
}
博客來網路書店查詢
星期六, 5月 10, 2008
Win2003課程第三次到第五次的簡報檔案
這裡提供win2003課程第三次到第五次的簡報檔案,
不過這裡先提供這三次課程的原簡報檔案,
在士林分校上課所抓取的畫面,我稍後再做簡報介紹。
內容為使用者管理、時間同步、群組管理。
因下載的節點在國外,所以速度略慢,
另外,若九十天內沒有人下載,下載節點就會消失。
第三次上課原設計的簡報(上課抓取的畫面於日後再提供)
第四次上課原設計的簡報(上課抓取的畫面於日後再提供)
第五次上課原設計的簡報(上課抓取的畫面於日後再提供)
不過這裡先提供這三次課程的原簡報檔案,
在士林分校上課所抓取的畫面,我稍後再做簡報介紹。
內容為使用者管理、時間同步、群組管理。
因下載的節點在國外,所以速度略慢,
另外,若九十天內沒有人下載,下載節點就會消失。
第三次上課原設計的簡報(上課抓取的畫面於日後再提供)
第四次上課原設計的簡報(上課抓取的畫面於日後再提供)
第五次上課原設計的簡報(上課抓取的畫面於日後再提供)
String (C語言)
請看以下的程式:
#include
#include
main(){
char *s3="Welcome";
char *s5="Good morning";
s3=s5;
printf("s3=%s\n",s3);
printf("s5=%s\n",s5);
s5="123";
printf("s3=%s\n",s3);
system("PAUSE");
return 0;
}
我們一行一行看...
char *s3="Welcome";
char *s5="Good morning";
s3與s5 沒有配置記憶體空間,"Welcome"與"Good morning"
算是編譯器在編譯期間就決定字串的位址,並沒有真正一塊記憶體
存放 "Welcome" & "Good morning"。
s3=s5;
上面這一行的意思是s3將指標指向的位址指向s5現在指向的位址
,也就是s3指標會指向 "Good morning"這一個字串位址。
s5="123";
上面這一行因為 "123"沒有記憶體空間,所以把 s5 改成 "123",
而s3 還是不變,也就是說s5指標指向 "123"這個字串的記憶體位置。
所以並沒有修改原有字串的內容。
指標要用 malloc 先配置記憶體,才可以在改變存在 malloc 產生的記憶體裡面的字串。
若是要進行字串複製動作,c與c++內不是使用=來指定,而是使用 strcpy()。
例如:
#include
#include
main(){
char s8[]="Good morning";
printf("s8=%s\n",s8);
strcpy(s8,"this is a testing");
printf("s8=%s\n",s8);
system("PAUSE");
return 0;
}
不過下面的複製字串寫法會有問題的,因為 s3是指標但是沒有指向任何合法
可提供寫入的記憶體內。
#include
#include
main(){
char *s3="Good morning";
printf("s3=%s\n",s3);
strcpy(s3,"this is a testing");
printf("s3=%s\n",s3);
system("PAUSE");
return 0;
}
所以常用的方式就是將指標指向合法可用的記憶體區段內再來存取,
使用 malloc() 配置記憶體提供使用:
區段內都可以解決。
#include
#include
main(){
char *s3=(char *) malloc(sizeof(char) * 100);
strcpy(s3,"this is a testing");
printf("s3=%s\n",s3);
system("PAUSE");
return 0;
}
指向其他可以寫入的記憶體:
#include
#include
main(){
char *s3;
char s8[]="Good morning";
s3="Welcome";
printf("s3=%s\n",s3);
printf("s8=%s\n",s8);
s3 = s8;
strcpy(s3,"this is a testing");
printf("s8=%s\n",s8);
printf("s3=%s\n",s3);
system("PAUSE");
return 0;
}
綜合以上的說明,如果是兩個指標,可以用以下的方修改字串資料:
#include
#include
main(){
char *s3,*s5;
s3=malloc(10 * sizeof(char));
strcpy(s3,"Welcome");
s5=malloc(15 * sizeof(char));
strcpy(s5,"Good morning");
printf("first s3=%s\n",s3);
printf("first s5=%s\n",s5);
s3=s5;
printf("second s3=%s\n",s3);
printf("second s5=%s\n",s5);
strcpy(s5,"123");
printf("third s3=%s\n",s3);
printf("third s5=%s\n",s5);
system("pause");
return 0;
}
#include
#include
main(){
char *s3="Welcome";
char *s5="Good morning";
s3=s5;
printf("s3=%s\n",s3);
printf("s5=%s\n",s5);
s5="123";
printf("s3=%s\n",s3);
system("PAUSE");
return 0;
}
我們一行一行看...
char *s3="Welcome";
char *s5="Good morning";
s3與s5 沒有配置記憶體空間,"Welcome"與"Good morning"
算是編譯器在編譯期間就決定字串的位址,並沒有真正一塊記憶體
存放 "Welcome" & "Good morning"。
s3=s5;
上面這一行的意思是s3將指標指向的位址指向s5現在指向的位址
,也就是s3指標會指向 "Good morning"這一個字串位址。
s5="123";
上面這一行因為 "123"沒有記憶體空間,所以把 s5 改成 "123",
而s3 還是不變,也就是說s5指標指向 "123"這個字串的記憶體位置。
所以並沒有修改原有字串的內容。
指標要用 malloc 先配置記憶體,才可以在改變存在 malloc 產生的記憶體裡面的字串。
若是要進行字串複製動作,c與c++內不是使用=來指定,而是使用 strcpy()。
例如:
#include
#include
main(){
char s8[]="Good morning";
printf("s8=%s\n",s8);
strcpy(s8,"this is a testing");
printf("s8=%s\n",s8);
system("PAUSE");
return 0;
}
不過下面的複製字串寫法會有問題的,因為 s3是指標但是沒有指向任何合法
可提供寫入的記憶體內。
#include
#include
main(){
char *s3="Good morning";
printf("s3=%s\n",s3);
strcpy(s3,"this is a testing");
printf("s3=%s\n",s3);
system("PAUSE");
return 0;
}
所以常用的方式就是將指標指向合法可用的記憶體區段內再來存取,
使用 malloc() 配置記憶體提供使用:
區段內都可以解決。
#include
#include
main(){
char *s3=(char *) malloc(sizeof(char) * 100);
strcpy(s3,"this is a testing");
printf("s3=%s\n",s3);
system("PAUSE");
return 0;
}
指向其他可以寫入的記憶體:
#include
#include
main(){
char *s3;
char s8[]="Good morning";
s3="Welcome";
printf("s3=%s\n",s3);
printf("s8=%s\n",s8);
s3 = s8;
strcpy(s3,"this is a testing");
printf("s8=%s\n",s8);
printf("s3=%s\n",s3);
system("PAUSE");
return 0;
}
綜合以上的說明,如果是兩個指標,可以用以下的方修改字串資料:
#include
#include
main(){
char *s3,*s5;
s3=malloc(10 * sizeof(char));
strcpy(s3,"Welcome");
s5=malloc(15 * sizeof(char));
strcpy(s5,"Good morning");
printf("first s3=%s\n",s3);
printf("first s5=%s\n",s5);
s3=s5;
printf("second s3=%s\n",s3);
printf("second s5=%s\n",s5);
strcpy(s5,"123");
printf("third s3=%s\n",s3);
printf("third s5=%s\n",s5);
system("pause");
return 0;
}
星期一, 5月 05, 2008
2008年5月:東區認證中心 ASP.Net 進階課程
課程將進入到資料庫的階段,資料庫將以Access作為操作的對象,
瞭解網頁資料如何傳送到資料庫內與從資料庫讀取資料。
最後會以範例來貫穿課程。
如果有疑問或課程建議,請提出來!
如果沒有回應,那我這篇文章一個月後會找時間刪除!!
瞭解網頁資料如何傳送到資料庫內與從資料庫讀取資料。
最後會以範例來貫穿課程。
如果有疑問或課程建議,請提出來!
如果沒有回應,那我這篇文章一個月後會找時間刪除!!
星期六, 5月 03, 2008
Win2003第一次與第二次上課簡報
訂閱:
文章 (Atom)