[英]warning: dereferencing ‘void *’ pointer [enabled by default] and error: void value not ignored as it ought to be help c
I'm confused about these errors.我对这些错误感到困惑。 The str1 is a string that is being passed but I get the a warning at the compare and and error at the if statement
str1 是一个正在传递的字符串,但我在比较时收到警告,在 if 语句中出现错误
int stringcmp(void* str1, void* str2) {
int a = strlen(str1);
int b = strlen(str2);
int x;
if ( a < b ) {
x = a;
} else {
x = b;
}
int c = 0;
while ( c < x ) {
if (str1[c] < str2[c]) { //errors happen here
return 0;
}
if (str1[c] > str2[c]) {
return 1;
}
c++;
}
if ( a == x ) {
return 0;
}
return 1;
}
Your function receives void*
arguments, so in the line you pointed you are dereferencing pointers to void
and that is why you get the warnings.您的函数接收
void*
参数,因此在您指出的行中,您正在取消引用指向void
指针,这就是您收到警告的原因。 I am not sure why you are receiving the value not ignore as it ought to be
because that means that you are assigning the value of a function that returns void, and that is not the case of strlen (which is the only one you are calling).我不确定为什么您收到的
value not ignore as it ought to be
因为这意味着您正在分配一个返回 void 的函数的值,而 strlen 的情况并非如此(这是您正在调用的唯一函数) )。
And you should also receive an error in the calls to strlen when passing to it a void*
parameter.并且您还应该在向 strlen 传递
void*
参数时在调用中收到错误。
So you have to change your function's signature to所以你必须将函数的签名更改为
int stringcmp(const char* str1, const char* str2);
to suppress the warnings and be able to call strlen on the strings.抑制警告并能够在字符串上调用 strlen 。
Dereferencing void *
makes no sense, ever. void *
引用void *
永远没有意义。 In C, void
means "no type"/"nothing", if you have void *p;
在 C 中,
void
表示“无类型”/“无”,如果你有void *p;
, what is *p
supposed to be? ,
*p
应该是什么?
In C, void *
is used as a generic pointer type, "pointer to anything".在 C 中,
void *
用作通用指针类型,“指向任何东西的指针”。 To use p
above, you must cast it to the type of the object it is pointing at (passed in some other way, unbeknownst to the compiler).要使用上面的
p
,您必须将其强制转换为它指向的对象的类型(以其他方式传递,编译器不知道)。 Eg:例如:
int i;
void *p = (void *) &i;
...
int j = *(int *)p;
You know what p
points to, the compiler doesn't.你知道
p
指向什么,编译器不知道。
The type void *
is often used to pass around opaque data, or to write generic functions (like qsort
, it gets an array of unspecified elements and a function that compares them). void *
类型通常用于传递不透明数据,或编写通用函数(如qsort
,它获取未指定元素的数组和比较它们的函数)。
That said, as a (misguided) extension GCC allows pointer arithmetic on void *
, so that p + 1
is like ((char *)p + 1)
, it points at the next char
position.也就是说,作为(被误导的)扩展 GCC 允许对
void *
进行指针运算,因此p + 1
就像((char *)p + 1)
,它指向下一个char
位置。
Your error arises from the fact that you are attempting to use the array notation str1[c]
on a void-pointer.您的错误源于您试图在空指针上使用数组符号
str1[c]
。 To understand why this is wrong we need to look at what the array notation in C actually means.要理解为什么这是错误的,我们需要看看 C 中数组符号的实际含义。
Let us assume that we define char* str1
.让我们假设我们定义了
char* str1
。 Here we have said that str1
is an address to some place in memory where there is a char
.这里我们说过
str1
是内存中某个有char
地方的地址。 To get the data that is stored on the address which str1
is referring to we can use *str1
.要获取存储在
str1
所指地址上的数据,我们可以使用*str1
。 This is equivalent to saying: "Go to the address that str1
is holding and give me what is stored on that address".这相当于说:“转到
str1
持有的地址,并给我存储在该地址上的内容”。
When we use the array notation we can use str1[0]
, this will be a value fetched from a place in memory where there is an element of the same type that str1
was defined as.当我们使用数组表示法时,我们可以使用
str1[0]
,这将是从内存中某个位置获取的值,该位置存在与str1
定义为相同类型的元素。 It is the same thing as saying *str1
(go to the address that str1
is pointing to and give me the value that is stored there).这与说
*str1
相同(转到str1
指向的地址并给我存储在那里的值)。
An array is just a bunch of data stored in a sequence in memory and strings are just arrays of characters of exactly 1 byte in size stored immediately after one another.数组只是存储在内存中的一系列数据,而字符串只是大小正好为 1 个字节的字符数组,它们立即一个接一个地存储。 When we say
str1[1]
we are telling the compiler to move by the size of the type that str1
was defined to be pointing at, in this case 1 byte( char
), and then get us whatever is stored at that location.当我们说
str1[1]
我们告诉编译器移动str1
定义为指向的类型的大小,在本例中为 1 字节( char
),然后获取存储在该位置的任何内容。 In the case of strings, this should be another char
.在字符串的情况下,这应该是另一个
char
。
Now when we have defined str1
as void*
, how would the compiler know how how much it should move in memory to get the next element in the array?现在,当我们将
str1
定义为void*
,编译器如何知道它应该在内存中移动多少才能获得数组中的下一个元素? Since void has no size it is impossible.因为 void 没有大小,所以这是不可能的。
Hopefully you now understand what you need to change in this line to get rid of your errors希望您现在了解在这一行中需要更改哪些内容以消除错误
int stringcmp(void* str1, void* str2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.