char **p = malloc(n * sizeof(char*));p[i] = malloc(len * sizeof(char));#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **alloc_strings(int n, int max_len) {
char **p = malloc(n * sizeof(char*));
if (!p) return NULL;
for (int i=0; i
// 正確
for (i=0; i
若字串最大長度固定,可一次配置大塊:char *buf = malloc(n * max_len); char **p = malloc(n*sizeof(char*)); for(i) p[i]=buf+i*max_len; 釋放時只需 free(buf); free(p); 兩次呼叫,減少碎片與系統呼叫。
argv 本質即 char**)。