Void * type conversion to string

A type of conversion experience problems when I try to type void * is converted to a string type b, write:

. 1  . 1  void * A;
 2  2  String B;
 . 3  . 3 B = ( String ) (A *); // Error

 

The following error:

error: 'void *' is not a pointer-to-object type (void * object does not point type)

Correct codes:

void *a;
string b;
b = *(string*)a;

The first converted into a string and then dereferenced pointer, assigned to B (solution string pointer for a reference string type is obtained, of course).

Q pulling the root end (C / C ++):

  void * means "null pointer type" denotes: any type of pointer, or the pointer associated with an address value, but the type is not clear at this object's address.

  Can not directly dereference void * type, because the type of void variable does not exist.

  void keyword represents "empty Type" indicates the absence of means, not intended to represent any type, it can be modified with a void function (  void A ( void );  represents the function returns no value or the returned value is null  return ;  , the function parameter position indicates no function parameter), but can not modify variables void, that is not written statement  void a;  , what type void does not exist.

  Further, why can not there is a variable of type void, or why in the face of variables, void can not be said that "any type" mean?

  We know C / C ++ is a statically typed language, define variables will allocate memory, but the share of different variables of different types of memory, if you define a variable of any type, how to assign memory?

  So, C, C ++ is not any type of variable. However, all types of pointer variable, both int *, char *, string *, Student *, etc., they are the same memory space, so you can define "any type of pointer", share memory with the pointer type For addressing capacity of the system, for example, 32-bit system occupies 4 bytes, 64-bit system occupies 8 bytes ......

 

Guess you like

Origin www.cnblogs.com/qzwjer/p/11106401.html