我正在编写一个代码,它接受用户想要的长度的字符串并显示它。所以这就是我写的
#include<stdio.h>
#include<stdlib.h>
void main()
{
char *a;
int i,n;
a=(char*)calloc(1,sizeof(char));
printf("enter the string\t");
for(i=0;a[i]!=0;i++)
{
scanf("%[^0]c",&a[i]);
n=2+i;
a=(char*)realloc(a,(n)*sizeof(char));
}
for(i=0;a[i]!=0;i++)
{
printf("%c",a[i]);
}
free(a);
a=NULL;
}
但它没有进入循环并接受任何输入。你能告诉我出了什么问题吗?谢谢
最佳答案
您没有进入循环,因为您使用 calloc
分配了 a
,因此 a[0]
为 0。它将导致初始失败a[i]!=0
测试两个 for
循环。
关于c - 我已经编写了这段代码,但它无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47893315/