search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[C/C++基礎]- 59_純C語言動態配製記憶體和動態調整記憶體大小_實作(字串陣列) – jashliao部落格

[C/C++基礎]- 59_C語言動態配置記憶體和動態調整記憶體大小_實作(字串陣列)

本篇要分享C語言動態配置記憶體和動態調整記憶體大小_實作(字串陣列),有興趣的(C/P)同好,歡迎來(C/P)一下哈哈 ^ ^

 
#include 
 
 
 
using namespace std;
 
 
 
//Dynamic string arrays
 
 
 
#include 
 
#include 
 
#include 
 
 
 
int main (void) {
 
 char **strarray = NULL;
 
 int i = 0, strcount = 0;
 
 char line[1024];
 
 int j=0;
 
 while(j<3) {
 
  fgets(line, 1024, stdin);
 
  strarray = (char **)realloc(strarray, (strcount + 1) * sizeof(char *));
 
  strarray[strcount++] = strdup(line);//strcpy(strarray[strcount++], line);
 
  j++;
 
 }
 
 
 
 /* print the array of strings */
 
 for(i = 0; i < strcount; i++)
 
  printf("strarray[%d] == %s", i, strarray[i]);
 
 
 
 /*
 
 // free the string array
 
 // Note: You must delete each individual string
 
 //       before you delete the array of pointers
 
 */
 
 for(i = 0; i < strcount; i++)
 
  free(strarray[i]);
 
 
 
 free(strarray);
 
 return 0;
 
}
 

 

 

 



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦