【发布时间】:2021-12-28 20:57:25
【问题描述】:
请大家告诉我的代码有什么问题。 我已经添加了 cs50 库和头文件,但似乎无法正确执行。 我是初学者,想听听您的建议。
代码:-
#include <stdio.h>
#include <cs50.c>
#include <string.h>
int main(void)
{
string s = get_string("Input: ");
printf("Output: ");
int n = strlen(s);
for( int i = 0; i < n; i++)
{
printf("%c", s[i]);
}
printf("\n");
}
错误:-
3.c: In function 'main':
3.c:7:27: warning: passing argument 1 of 'get_string' from incompatible pointer type [-Wincompatible-pointer-types]
7 | string s = get_string("Input: ");
| ^~~~~~~~~~~
| |
| char *
In file included from 3.c:2:
C:/msys64/mingw64/x86_64-w64-mingw32/include/cs50.c:78:28: note: expected 'char **' but argument is of type 'char *'
78 | string get_string(va_list *args, const char *format, ...)
| ~~~~~~~~~^~~~
3.c:7:16: error: too few arguments to function 'get_string'
7 | string s = get_string("Input: ");
| ^~~~~~~~~~
In file included from 3.c:2:
C:/msys64/mingw64/x86_64-w64-mingw32/include/cs50.c:78:8: note: declared here
78 | string get_string(va_list *args, const char *format, ...)
【问题讨论】:
-
您使用的是 C 还是 C++ 编译器?顺便说一句,如果该库与大多数其他库一样,您应该包括
cs50.h,而不是cs50.c。 -
我正在运行 C 编译器,当我尝试同时运行两者时,cs50.c 在 VS Code 上工作而不是 cs50.c。跨度>
-
您不应该(极少)包含
.c文件。您应该像documentation 所说的那样包含头文件。另外,@kaylum 在下面说了什么。 (不幸的是,我今天不能更多地投票给 cmets) -
@MokshBansal 不不,没问题。只需包含
cs50.h并将-lcs50添加到编译命令的末尾即可。gcc -o executable source.c -lcs50。如果cs50库未安装在标准搜索位置,您也需要提供该位置:gcc -o executable source.c -Lpath_to_libcs50 -lcs50 -
@MokshBansal 好的,如果您关注了该视频,您可能没有图书馆。然后您可以将
cs50.c添加到编译行:gcc -o executable source.c cs50.c