Zi 字媒體
2017-07-25T20:27:27+00:00
Linux C_純C/C++語言動態配製記憶體和動態調整記憶體大小_實作(字串陣列)『動態調整記憶體大小_字串陣列實作 標準參考範本』
GITHUB: https://github.com/jash-git/Jash_LinuxC/tree/master/純C語言動態配製記憶體和動態調整記憶體大小_實作(字串陣列)『動態調整記憶體大小_字串陣列實作 標準參考範本』
code:
//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;
}
寫了
5860316篇文章,獲得
23313次喜歡