Linux C_純C/C++語言動態配製記憶體和動態調整記憶體大小_實作(字串陣列)『動態調整記憶體大小_字串陣列實作 標準參考範本』
Linux C_純C/C++語言動態配製記憶體和動態調整記憶體大小_實作(字串陣列)『動態調整記憶體大小_字串陣列實作 標準參考範本』
GITHUB: https://github.com/jash-git/Jash_LinuxC/tree/master/%E7%B4%94C%E8%AA%9E%E8%A8%80%E5%8B%95%E6%85%8B%E9%85%8D%E8%A3%BD%E8%A8%98%E6%86%B6%E9%AB%94%E5%92%8C%E5%8B%95%E6%85%8B%E8%AA%BF%E6%95%B4%E8%A8%98%E6%86%B6%E9%AB%94%E5%A4%A7%E5%B0%8F_%E5%AF%A6%E4%BD%9C(%E5%AD%97%E4%B8%B2%E9%99%A3%E5%88%97)%E3%80%8E%E5%8B%95%E6%85%8B%E8%AA%BF%E6%95%B4%E8%A8%98%E6%86%B6%E9%AB%94%E5%A4%A7%E5%B0%8F_%E5%AD%97%E4%B8%B2%E9%99%A3%E5%88%97%E5%AF%A6%E4%BD%9C%20%E6%A8%99%E6%BA%96%E5%8F%83%E8%80%83%E7%AF%84%E6%9C%AC%E3%80%8F
code:
//Dynamic string arrays #include <stdio.h> #include <stdlib.h> #include <string.h> 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; }