【发布时间】:2020-05-02 13:48:06
【问题描述】:
我试图从用户那里获得 2 个长度未知的字符指针的输入。(处理动态分配内存)但是在使用“空格”输入第一个输入后,它不会等待用户输入第二个输入,它只读取一个单词,然后允许第二个输入。
char *str1;
char ch;
printf("Enter a sentence:(Ex: Computer Engineer)");
str1 = (char*)malloc(sizeof(char*));
scanf(" %s", str1);
printf("Enter a character to search(Ex: g):");
scanf(" %c", &ch);
char *result;
result=mystrchr(str1,ch);
if(result!=NULL)
printf("%s",result);
else
printf("NULL");
【问题讨论】:
-
你应该not cast malloc in C...
-
考虑使用getline(3)
标签: c malloc scanf dynamic-memory-allocation