<分区>
这是我的代码:
int main() {
int i=0;
int size=1;
char *pntName=NULL;//a pointer to an array of chars.
pntName = (char*) malloc(size *sizeof(char));//allocate sapce for the first char.
while(pntName[size-1]!=':'){
if(pntName!=NULL)//check the case couldn't allocate
printf("Error");
else{
if(i<size){//meaning there is space for new char.
scanf("%c",&pntName[i]);
i++;
}
else{//case we don't have enough space
size++;
pntName = (char*)realloc(pntName,(size)*sizeof(char));//reallocat space.
scanf("%c",&pntName[i]);
i++;
}
}
}
return 1;
}
我正在尝试读取一个包含名称的字符串。用户可以输入字符,直到他输入':'。 我的代码有什么问题?