简体   繁体   中英

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

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

And you should also receive an error in the calls to strlen when passing to it a void* parameter.

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.

Dereferencing void * makes no sense, ever. In C, void means "no type"/"nothing", if you have void *p; , what is *p supposed to be?

In C, void * is used as a generic pointer type, "pointer to anything". 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). Eg:

int i;
void *p = (void *) &i;
...
int j = *(int *)p;

You know what p points to, the compiler doesn't.

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

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.

Your error arises from the fact that you are attempting to use the array notation str1[c] on a void-pointer. To understand why this is wrong we need to look at what the array notation in C actually means.

Let us assume that we define char* str1 . Here we have said that str1 is an address to some place in memory where there is a char . To get the data that is stored on the address which str1 is referring to we can use *str1 . This is equivalent to saying: "Go to the address that str1 is holding and give me what is stored on that address".

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

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. 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. In the case of strings, this should be another 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? Since void has no size it is impossible.

Hopefully you now understand what you need to change in this line to get rid of your errors

int stringcmp(void* str1, void* str2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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