訪客只能看到部份內容,免費 加入會員 或由臉書 Google 可以看到全部內容
下面是引用ducktw於2008-11-03 23:02(樓主)發表的 請教指標用法【已自行解決】: 經過兩天努力,終於勉強拼出來了,貼出來請先進指教,看能否提供更好的寫法,謝謝。 指標用法 #include "stdafx.h" #include <iostream> .......
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc,char** argv) { int i=0,no=0,flag=1,word=-1,buf_size=256; char c; char *temp=NULL,*ptr; char **name=NULL; temp=(char*)malloc(sizeof(char)*buf_size);/*buffer size=255 256='\0'*/ while(flag) { printf("Please input the number what you want enter:"); scanf("%d",&no); if(no>0) flag=0; } getchar(); /* 處理沒被吃掉的enter鍵(也就是'\n') */ name=(char**)malloc(sizeof(char*)*no); for(i=0;i<no;i++) { printf("Please input NO.%d name:",i+1); word=-1; /* 每次開始的初使化動作 */ temp=(char*)malloc(sizeof(char)*buf_size); /*開空間,預設為256,若有超過 每次開超過一倍空間 */ while((c=getchar())!='\n') { word++; if(word<buf_size) temp[word]=c; else /* 超過buf_size 則新開多一倍的空間來儲放 */ { ptr=temp; buf_size*=2; temp=(char*)malloc(sizeof(char)*buf_size); strcpy(temp,ptr); free(ptr); /* 將舊有不用的空間還回去 */ temp[word]=c; } } temp[word+1]='\0'; /* 確定結尾必為'\0' */ name[ i ]=(char*)malloc(sizeof(char)*strlen(temp)+1); strcpy(name[i],temp); free(temp); } for(i=0;i<no;i++) { printf("No.%d %s\n",i+1,name[i]); } for(i=0;i<no;i++) /*用完的空間要還回去 */ free(name[i]); system("PAUSE"); return 0; }
下面是引用WindinCloud於2008-11-12 00:53(2樓)發表的 : 我覺得不用太拘泥指標跟陣列某種程度上你應該要先理解 array 與 pointer 有啥相關以下的是我改過的程式.......
下面是引用teldeanmac85於2008-11-12 13:16(3樓)發表的 : 請問程式真的能work嗎? 有點問題耶~
下面是引用三仙於2008-11-12 14:14(4樓)發表的 : 1.請問程式真的能work嗎?ans.能不能 run ,歡迎告知!! 如有問題,希望對這方面有了解的 能夠提出具體的看法,讓大家都能學習到 如果對問題有幫助,都會有評分的
下面是引用WindinCloud於2008-11-12 17:02(6樓)發表的 : 我幫t大平反一下 的確有問題 問題出在系統 因為他把我程式中"[ i ]" 變成斜體 這樣自然會出問題了 版大可以幫我反應一下嘛? XD
Wind Code 中 [i] 會被當成斜體字 建議使用 [code] your statement [/code ]