c - 决定尝试一下 C 中的自制函数

标签 c

我正在尝试创建一个获取字符串的函数。我成功创建了一个名为 mult() 的函数,它将数字相乘。您可以在这里找到我的代码:

#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME_SIZE
char *GetString(void);
int main (void)
{
    char *a;
    printf("give me your name please\n");
    a = GetString();
    printf("Why hello their, %s", a);
}
char *GetString(void)
{
    char x;
    fgets(x, MAX_NAME_SIZE, stdin);
}

这是我的错误

cc     string.c   -o string
string.c.c: In function ‘GetString’:
string.c:15:24: error: expected expression before ‘,’ token
 fgets(x, MAX_NAME_SIZE, stdin);
               ^
string.c:15:24: warning: passing argument 1 of ‘fgets’ makes pointer from integer without a cast [enabled by default]
In file included from string.c:1:0:
/usr/include/stdio.h:622:14: note: expected ‘char * __restrict__’ but argument is of type ‘char’
 extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
      ^
make: *** [string] Error 1
<小时/>

已解决。
我通过更改 char x 解决了这个问题;到 char *x 并让 GetString 返回 x,我还向 MAX_NAME_SIZE 添加了一个大小。

最佳答案

你的宏#define是错误的;尝试使用

     #define MAX_NAME_SIZE   1024

(或其他数字,也许是 128)

你应该使用gcc -Wall -g string.c -o string进行编译;顺便说一句,您可以使用 gcc -C -E string.c

获得预处理器输出

关于c - 决定尝试一下 C 中的自制函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23809763/

相关文章:

c - 在开关中分配结构不起作用

c - C 套接字编程使服务器无法识别连接

c - ACM 10038 Jolly 套头衫

c - 如何在障碍处正确同步线程

android - 如何从 unsigned char* 数组中获取 jpeg 图像数组?

c - 为什么使用偏移密码加密后文件大小变小了?

c - 声明为 void* foo() 的函数

c - 乘法的算法复杂度

c - 为什么对结构使用不同的不同标记和 typedef?

c - 打印数组中的字符