c - 在 main 中打印返回字符串的(完整)值

标签 c

嗨,我对“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/

相关文章:

c - 另一个C题

C/select 只读取一个文件描述符

c# - 在代码隐藏中调用 javascript

c - X86 PC 上的 RTLinux

java - Swig 如何定义一个类型映射,将 C 类型定义的 boolean 值转换为 java boolean 值

c - 为什么在我的程序中 "cat"一个FIFO特殊文件或创建一个FIFO特殊文件后没有出现shell提示?

c - 在 c [BEGINNER] 中使用 malloc 的堆栈实现

c - 单片机上的uart软件阅读——代码理解

c - 无需 calloc/malloc 的数组的动态内存分配

c - 如何从 C 中的 float 中提取有偏差的指数?