我正在尝试创建一个获取字符串的函数。我成功创建了一个名为 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/