About void pointers

Asked 1 years ago, Updated 1 years ago, 133 views

In the Description C pointer

Pointers to void are not equal to other pointers.(The behavior of the void pointers depends on the system.) However, if the pointers to the two void are both null pointers, they are considered equal.

There was a description that, but in what respect is it not equal that the void pointer is not equal to other pointers?

c pointer

2022-09-30 21:38

3 Answers

No. 1) It may be a typo or translation error.

Isn't the pointer to void the same as any other pointer?

 int func (void*a, int*b)
{
   long i;
   intj;

   i=*(long*)a;// cast required
   j=*b;// No cast required
...
   return j;
}

f Assume that you know that the entity passed by type void* where the func() function is the first argument is type long.

If the two void* types had exactly the same address

If the types to be cast are the same, they are equal pointers.
Different types to cast are different pointers.


2022-09-30 21:38

It's too late, but
O'Reilly's "Understanding and Using CP Pointers: Richard Reese" has the following parts:
The parentheses are slightly modified versions of Google's automated translation for programmers.

//// Citation (translation) begins

Pointer to void

A pointer to void is a general-purpose pointer used to hold references
to any data type.An example of a pointer to void is show below:
(A pointer to void is a generic pointer used to maintain references to any data type.)
(Examples of pointers to void are shown below.)

void*pv;

It has two interesting properties:

·A pointer to void will have the same presentation and memory alignment as a pointer to char.
(·The pointer to void has the same expression and memory alignment as the pointer to char.)

·A pointer to void will never be equal to another pointer. However, two void pointers assigned a NULL value will be equal.
(★) (·The pointer to void will not be the same as other pointers.However, the two void pointers with NULL values are equal.)
Quote ends at ///// or above

If you add (★), the translation will certainly not be translated.
However, the subsequent content explains the qualities that can be treated the same as those that can be treated the same, so
Maybe I should have translated it a little more carefully.For example,

Example 1 (pointer to void should not be considered the same as other pointers)
Example 2 (pointers to void cannot be treated the same as other pointers)

and so on.If it's like this, it's hard to misunderstand.


2022-09-30 21:38

I thought Arai's reply was a comment, but I don't trust him;
Unlike other pointers (char, etc.), void pointers cannot add or subtract (p+1) at all.This is because I don't know the data type, so I don't know how many bytes to proceed.


2022-09-30 21:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.