我需要一个只能在一个维度上动态生成的多维字符数组...
我必须存储一对长度为10(或更少)的字符串,但每个字符串的长度都是可变的“对”。Dynamic Multidimensional array
我的想法是这样的
char (*instrucao)[2][10];
,给了我一个指向字符的2×10阵列,但是当我做这样的事,这是不正常:
char strInstrucoes[117], *conjunto = calloc(21, sizeof(char));
instrucao = calloc(1, sizeof(char[2][10]));
conjunto = strtok(strInstrucoes,"() ");
for(i = 0; conjunto != NULL; i++){
realloc(instrucao, i+1*sizeof(char[2][10]));
sscanf(conjunto,"%[^,],%s", instrucao[i][0], instrucao[i][1]);
printf("%s | %s\n", instrucao[i][0], instrucao[i][1]);
conjunto = strtok(NULL, "() ");
}
有strInstrucoes
为(abc,123) (def,456) (ghi,789)
,我没有3行2对每个像这样的矩阵:
abc | 123
def | 456
ghi | 789
但相反,这是我得到:
abc | 123
def | 45def | 45de
ghi | 789
什么是正确的方法来做到这一点? 谢谢!
绝对正确...并通过我的旧代码看,我注意到我从来没有实际分配的指针realloc。好东西,我不用C专业工作,那么!非常感谢 – Gabe 2009-06-21 21:48:03