在C中将const void *转换为const char * [英] Casting a const void * to a const char * in C

查看:559
本文介绍了在C中将const void *转换为const char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个像这样的C函数:

So I have a C function that goes like this:

int cmp (const void *a, const void* b)
 return rot13cmp( (const char*)a, (const char*)b );
}

和rot13cmp是另一个函数,它带有两个类型为const char *的参数。

and rot13cmp is another function that takes two parameters of type const char *.

我将此函数传递给C的比较参数 qsort 函数,但似乎不起作用。

I pass this function into the compare parameter for the C qsort function but it doesn't seem to work.

但是,如果我改为通过执行

However, if I instead cast the const void * variables by doing

return rot13cmp ( *(const char **)a, *(const char **)b ); 

然后该功能开始工作。我看了一下SO,但是每个消息来源都说第一种铸造方法应该起作用,所以我想知道为什么只有第二种铸造方法对我有用?

the function then starts to work. I looked this up at SO but every source said that the first method of casting should work so I wanted to know why only the second one worked for me?

编辑:这是我有相关的代码,

Here's the relevant code I have,

int cmp (const void *a, const void *b) {
 return rot13cmp( (const char *)a, (const char *)b );
}

int rot13cmp (const char *a, const char *b) {
 while (*a == *b && *a != '\n') {
  a++;
  b++;
 }

 if (*a == *b) { return 0; }
 else if (*a == '\n') { return 1; }
 else if (*b == '\n') { return 1; }
 else { return rot13(*a) - rot13(*b);
}

和rot13返回一个由13个字母旋转的字母的ASCII码的int在字母表中。

and rot13 returns an int for the ASCII code of a letter rotated by 13 letters in the alphabet.

我通过这样做叫qsort

I called qsort by doing

qsort(words, word_count, sizeof(char*), cmp);

其中word是char **的数组,而word_count是int。 cmp也是

where words is an array of char** and word_count is an int. cmp is also just

推荐答案

qsort()

如果您的数组包含 const char * ,则表示比较函数使用指向这些指针的指针进行调用,并且您必须相应地进行强制转换和取消引用。

If your array contains const char*, that means the comparison function is called with pointers to those pointers, and you have to cast and dereference accordingly.

使用(const char *)a 您将参数解释为好像是指向 const char 的指针。但事实并非如此。实际上,它是指向输入数组中的 const char * 的指针。

With (const char*)a you are interpreting the parameter as if it would be a pointer to const char. But it isn't. In reality it's a pointer to the const char* in the input array.

这就是为什么(const char **)a 是正确的强制转换,它将参数解释为指向 const char * 的指针。要进行字符串比较,您需要指向 const char * ,您可以通过使用 * 解引用强制转换值来访问该字符串。

That's why (const char**)a is the correct cast, it interprets the parameter as a pointer to a const char*. To do string comparison you want that pointed-to const char*, which you access by dereferencing the casted value with *.

您可以将其视为首先纠正类型(通过强制转换),然后访问指向的值(通过取消引用)。

You can think of it as first correcting the type (by casting), and then accessing the pointed-to value (by dereferencing).

两次尝试之间的区别在于,第二种情况会进行额外的取消引用。这很重要,因为 qsort()不会直接传递 const char * ,而是传递一个指向它的指针。因此,我们必须查看指向的值才能找到我们想要的。通过直接强制转换为 const char * ,我们只是声称该变量将包含这样的指针,由于不是这种情况,所以不会很好地结束。

The difference between the two attempts is that the second case does an additional dereference. This is important since qsort() doesn't pass the const char* directly, but rather passes a pointer to it. So we have to look at the pointed-to value to find what we are looking for. By casting directly to const char* we just claim that the variable would contain such a pointer, which won't end well because that's not the case.

这篇关于在C中将const void *转换为const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆