简体   繁体   English

将 const 与 typedef 和函数指针一起使用的问题

[英]Problem using const together with typedef and function pointer

Here is a MRE这是一个 MRE

#include <string.h>

typedef char* mytype;

typedef int(*cmp)(const mytype, const mytype);

void foo(cmp f) {}

int main(void) {
    foo(strcmp);
}

When I compile I get:当我编译时,我得到:

$ gcc mre.c -Wall -Wextra
mre.c: In function ‘foo’:
mre.c:7:14: warning: unused parameter ‘f’ [-Wunused-parameter]
    7 | void foo(cmp f) {}
      |          ~~~~^
mre.c: In function ‘main’:
mre.c:10:9: warning: passing argument 1 of ‘foo’ from incompatible pointer type [-Wincompatible-pointer-types]
   10 |     foo(strcmp);
      |         ^~~~~~
      |         |
      |         int (*)(const char *, const char *)
mre.c:7:14: note: expected ‘cmp’ {aka ‘int (*)(char * const,  char * const)’} but argument is of type ‘int (*)(const char *, const char *)’
    7 | void foo(cmp f) {}
      |          ~~~~^

The first warning is irrelevant.第一个警告无关紧要。 But what do I do about the second?但是第二个我该怎么办? I tried changing the typedef to:我尝试将 typedef 更改为:

typedef int(*cmp)(mytype const,mytype const);

But that gave the exact same result.但这给出了完全相同的结果。 When I changed to:当我改为:

typedef int(*cmp)(const char*, const char*);

it worked, but that's not preferable for obvious reasons.它有效,但由于显而易见的原因,这并不可取。 Another thing that worked but also is not preferable is this:另一件有效但也不可取的事情是:

typedef const char* mytype;

typedef int(*cmp)(mytype, mytype);

So what have I missed here?那么我在这里错过了什么?

The problem I'm trying to solve is that I want to create a generic structure where the data is of type mytype .我试图解决的问题是我想创建一个通用结构,其中数据的类型为mytype The user of the structure is expected to implement a compare function, but I want it to work with strcmp in case mytype is of type char* .该结构的用户应该实现一个比较函数,但我希望它与strcmp ,以防mytypechar*类型。

Note that mytype not necessarily needs to be a pointer.请注意, mytype不一定需要是指针。 It depends on what data type the user specifies.这取决于用户指定的数据类型。 Also, I know that typedefing pointers is bad practice in general, but I consider this a special case because I want to typedef whatever the type is, and it may be a pointer.另外,我知道 typedefing 指针通常是不好的做法,但我认为这是一个特殊情况,因为我想 typedef 无论类型是什么,它可能是一个指针。

The structure looks like this:结构如下所示:

struct node {
    struct node *next;
    mytype data;
};

I managed to solve it with #define mytype char* but that feels very ugly, and I would prefer if there was another way.我设法用#define mytype char*解决了它,但这感觉很丑陋,如果有另一种方法,我更愿意。 I want it to be portable, so I don't want to use gcc extensions.我希望它是可移植的,所以我不想使用 gcc 扩展。

typedef char* mytype;

mytype is a pointer to char . mytype是一个指向char的指针。 const mytype is a const pointer to char not pointer to const char. const mytype是指向 char 的 const 指针,而不是指向 const char 的指针。

Unfortunately in C you cant use typedef pointers and the use this type to declare pointer to const object.不幸的是,在 C 中你不能使用 typedef 指针,并且不能使用这种类型来声明指向const对象的指针。

you need to你需要

typedef int(*cmp)(const char *, const char *);

void foo(cmp f) {}

int main(void) {
    foo(strcmp);
}

or或者

    foo((cmp)strcmp);

BTW it a very bad practice to hide pointers behind the typedefs.顺便说一句,将指针隐藏在 typedef 后面是一种非常糟糕的做法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM