简体   繁体   English

错误:- 从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“get_string”的参数 1

[英]ERROR :- passing argument 1 of 'get_string' from incompatible pointer type [-Wincompatible-pointer-types]

guys please tell what's wrong with my code.伙计们请告诉我的代码有什么问题。 I have added the cs50 library and the header file but can't seem to do it correct.我已经添加了 cs50 库和 header 文件,但似乎无法正确执行。 I am a begginer and like to know your advice.我是一个初学者,想知道你的建议。

CODE:-代码:-

#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");
}

ERROR:-错误:-

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, ...)

The cs50 docs states you should be including cs50.h , not cs50.c . cs50 文档声明您应该包括cs50.h ,而不是cs50.c https://cs50.readthedocs.io/libraries/cs50/c/#c.get_char https://cs50.readthedocs.io/libraries/cs50/c/#c.get_string https://cs50.readthedocs.io/libraries/cs50/c/#c.get_char https://cs50.readthedocs.io/libraries/cs50/c/#c.get_string

You need to add the header file in your include path.您需要在包含路径中添加 header 文件。

It was already stated in comments and the previous answer that you should not include the cs50.c file but only include the cs50.h file and link the cs50.c file to your project.在评论和之前的答案中已经说明,您不应包含cs50.c文件,而应仅包含cs50.h文件并将cs50.c文件链接到您的项目。

That is generally true unless you have very specific reasons to do it differently.这通常是正确的,除非您有非常具体的理由采取不同的做法。

But... normally this should not cause the error we see in the question.但是......通常这不应该导致我们在问题中看到的错误。 This is because the cs50.c file included cs50.h on its own and we should have all definitions and declaration visible.这是因为cs50.c文件本身包含cs50.h ,我们应该让所有定义和声明可见。

In this specific case we hit some specific implementation detail of CS50 libary which comes a bit as a surprise.在这个特定的案例中,我们遇到了一些 CS50 库的特定实现细节,这有点令人惊讶。 And would not be necessary also...而且也没有必要...

Let's take a closer look at the header:让我们仔细看看header:

// cs50.h
string get_string(va_list *args, const char *format, ...) __attribute__((format(printf, 2, 3)));
#define get_string(...) get_string(NULL, __VA_ARGS__)

After these 2 lines we can use get_string as intented by the author of this question: string s = get_string("Input: ");在这两行之后,我们可以按照这个问题的作者的意图使用get_stringstring s = get_string("Input: ");

It is not really obvious why anyone might think it is a good idea to hide a function behind a macro with same name but different parameters.并不是很明显为什么有人会认为将 function 隐藏在具有相同名称但参数不同的宏后面是一个好主意。 In most other APIs that function would have a different name than the macro.在大多数其他 API 中,function 的名称与宏不同。 But, nevermind...但是没关系...

Now let's look at the C file:现在让我们看一下 C 文件:

// cs50.c
#include "cs50.h"
...
#undef get_string
string get_string(va_list *args, const char *format, ...)
{
...
}

If you compile this file on its own, everything is fine.如果你自己编译这个文件,一切都很好。 The .c file does not need the macro and can just get rid of it before defining the function. .c文件不需要宏,可以在定义 function 之前去掉它。

But if you include this in your own file, where you are supposed to use the macro instead, this is not possible any more.但是,如果您将其包含在您自己的文件中,而您应该在其中使用宏,则不再可能。 The undef breaks the API if you include the file directly.如果直接包含文件, undef会破坏 API。

This emphasises the fact that you should only include the headers.这强调了您应该只包含标题的事实。 They are meant to be included and they are made accordingly.它们应该被包括在内,并且是相应地制作的。 The .c files holding the implementation are not necessarily made that way, as we can see...正如我们所见,保存实现的.c文件不一定是这样制作的……

As a side note: This #undef is not necessary at all.附带说明:这个#undef根本没有必要。 You could simply do this instead:你可以简单地这样做:

// cs50.c
#include "cs50.h"
...
string (get_string)(va_list *args, const char *format, ...)
{
...
}

With enclosing () the indentifier get_string does not match the macro get_string() any more and no replacement is done.使用封闭()标识符get_string不再匹配宏get_string()并且不进行替换。 This would even allow to include the .c file directly.这甚至允许直接包含.c文件。

Maybe they chose that way intentionally to prevent including the c file, but I would not bet on that.也许他们故意选择这种方式是为了防止包含 c 文件,但我不会打赌。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“send_to_host”的参数 2 - passing argument 2 of ‘send_to_host’ from incompatible pointer type [-Wincompatible-pointer-types] 警告:从不兼容的指针类型 [-Wincompatible-pointer-types]| 传递 'transform_labels' 的参数 2 | - Warning: passing argument 2 of 'transform_labels' from incompatible pointer type [-Wincompatible-pointer-types]| 警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“接受”的参数 2 - warning: passing argument 2 of ‘accept’ from incompatible pointer type [-Wincompatible-pointer-types] 从不兼容的指针类型 [-Wincompatible-pointer-types] 获取传递“pthread_create”参数 3 的警告 - Getting warning of passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types] 来自不兼容指针类型的赋值[Wincompatible-pointer-types] - Assignment from incompatible pointer type[Wincompatible-pointer-types] 警告:从不兼容的指针类型返回 [-Wincompatible-pointer-types]| - warning: return from incompatible pointer type [-Wincompatible-pointer-types]| 来自不兼容的指针类型[-Wincompatible-pointer-types]的赋值 - assignment from incompatible pointer type [-Wincompatible-pointer-types] C中的函数指针:警告:来自不兼容指针类型[-Wincompatible-pointer-types]的赋值 - Function pointers in C: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] RecursiveFree函数-警告:从不兼容的指针类型[-Wincompatible-pointer-types]进行初始化 - RecursiveFree Function - Warning: initialization from incompatible pointer type [-Wincompatible-pointer-types] 如何修复将&#39;char [16]&#39;传递给&#39;FILE *&#39;类型的参数(又名&#39;struct __sFILE *&#39;)的不兼容指针类型[-Wincompatible-pointer-types] - how to fix incompatible pointer types passing 'char [16]' to parameter of type 'FILE *' (aka 'struct __sFILE *') [-Wincompatible-pointer-types]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM