C基本数据字符串返回功能 [英] C Basic String Return Function

查看:173
本文介绍了C基本数据字符串返回功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是pretty新C.我还是不明白指针一切都没有。
我试图使该返回一个字符串的方法。
这里的功能,它仍然是不完整的。

I'm still pretty new to C. I still don't understand everything with pointers at all. I'm trying to make a method that returns a String. Here's the function, it's still incomplete.

char getS(char *fileName){
    FILE *src;
    if((src = fopen(fileName, "r")) == NULL){
        printf("%s %s %s", "Cannot open file ", fileName, ". The program is now ending.");
        exit(-1);
    }
    char *get = " ";        
    //insert getting a random word here
    return(*get);
}

和我想这样调用方法

char *article = getS("articles.txt");
char *noun = getS("nouns.txt");
char *verb = getS("verbs.txt");

编译器是给我这样的:

The compiler is giving me this:

error: invalid type argument of unary ‘*’ (have ‘int’)

我应该怎么办?

推荐答案

您函数返回一个char *(字符串),而不是一个char,它应该返回相同。所以函数变为:

Your function should return a char * (a string), instead of a char, and it should return the same. So the function becomes:

char * getS(char *fileName) {
    FILE *src;
    if((src = fopen(fileName, "r")) == NULL) {
        printf("%s %s %s", "Cannot open file ", fileName, ". The program is now ending.");
        exit(-1);
    }

    char *get = " ";        
    //insert getting a random word here

    return get;
}

这篇关于C基本数据字符串返回功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆