【发布时间】:2012-05-08 09:35:23
【问题描述】:
我想读取数组中的一组字符串。数组的大小将在运行时使用malloc 或alloc 决定,即接受字符串的数量作为输入。我尝试了以下代码,但没有成功。
char *array;
array=(char*)malloc(sizeof(char)*t); //t is the size of array
for(int temp=0;temp<t;temp++)
{
gets(a[temp]);
}
在整数数组的情况下也是如此。
请帮助我找到解决方案。
【问题讨论】:
-
BTW sizeof(char) 是多余的;它被 C 标准定义并要求为 1。
-
@glglgl 对,sizeof(char) 是多余的。相反,我应该使用 malloc(1*t)。
-
为什么不只是
malloc(t)? :-)