我想用putchar()和getchar()从用户中读取一串字符,然后我的循环将打印每个字符三次,打印新行等等,直到变量“userInput”中的每个字符都已经打印完毕。当我尝试编译我的节目,我收到以下错误:使用putchar()和getchar()打印单个字符
warning: assignment makes pointer from integer without a cast [enabled by default]
userInput = getchar();
^
warning: comparison between pointer and integer [enabled by default]
while(counter < strlen(userInput))
^
error: array subscript is not an integer
putchar(userInput[counter]);
^
error: array subscript is not an integer
putchar(userInput[counter]);
^
error: array subscript is not an integer
putchar(userInput[counter]);
^
如何解决这些错误?我是C新手,无法弄清楚指针转换错误的意义,以及为什么我的计数器变量不起作用。
我的代码如下:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
char *userInput;
int *counter = 0;
printf("Enter a string of characters: ");
userInput = getchar();
while(counter < strlen(userInput))
{
putchar(userInput[counter]);
putchar(userInput[counter]);
putchar(userInput[counter]);
printf("\n");
counter++;
}
}
'getchar'读取*一个*字符,而不是一串字符。 – gurka