嗨,我对“C”语言很陌生,我正在努力打印函数 get_type_of_card()
的返回值,我想在同一行的 main 中打印它。如果我输入字符串 "VISA",则输出 = VISA。
截至目前,输出仅为 “VISA” 中的 'V'。
我收到一个警告,我想知道如何消除它。
警告:赋值从整数生成指针,无需强制转换 [-Wint conversion] card[i] = c;
我尝试使用 putchar()
进行 for 循环。
#include <stdio.h>
#include <ctype.h>
//Function prototypes
const char* get_type_of_card();
int main(void)
{
const char* type_of_crd = get_type_of_card();
printf("\nYou entered: %c", type_of_crd);
return 0;
}
const char* get_type_of_card()
{
const char* card[50];
char c;
int i;
do
{
printf("\nPlease select a credit card: ");
for (i = 0; (c = getchar()) != '\n'; ++i)
{
card[i] = c;
return card[i]; // Holds value of "VISA"
}
card[i] = '\0';
} while ((c != '\n') && (c != EOF));
}
最佳答案
我已经获得了结果,但不确定它在编程上是否正确。
#include <stdio.h>
#include <ctype.h>
//Function prototypes
const char* get_type_of_card();
int main(void)
{
const char* type_of_crd = get_type_of_card();
printf("\nYou entered: %s", type_of_crd);
return 0;
}
const char* get_type_of_card()
{
static char card[50];
char c;
int i;
do
{
printf("\nPlease select a credit card: ");
for (i = 0; (c = getchar()) != '\n'; ++i)
{
card[i] = c;
}
card[i] = '\0';
} while ((c != '\n') && (c != EOF));
return card;
}
关于c - 在 main 中打印返回字符串的(完整)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57614975/