Reprint--void pointer (void * usage)

Source: Internet
Author: User

Turn from: Jimmy

the pointer has two properties: point to the address and length of the variable/object
But the pointer only stores the address, and the length depends on the pointer's type   the
compiler addresses the   from the pointer's address according to the type of the pointer,
the pointer type is different and the addressing range is different, such as:  
int* Searches backwards from the specified address for a storage unit of 4 bytes as a variable  
double* searches backwards from the specified address for a storage unit of 8 bytes as a variable  

1.void Pointer is a special pointer  
    void *vp 
  /say it especially because it has no type  
  //or this type cannot determine the length of the pointer to the object  

2. Any pointer can be assigned to a void pointer  
   type *p; 
   vp=p; 
  //No conversion  
  //Obtain only the variable/object address without getting the size   the

3.void pointer is assigned to other types of pointers and is converted  
   type *p= (type*) VP;  
  //The type of conversion is to obtain a pointer to the variable/object size  
Ext: http://icoding.spaces.live.com/blog/cns!209684E38D520BA6 !130.entry 

4.void Pointer cannot re-reference  
   *vp//error  
   because void pointer only knows, point to variable/ The start address of the object is  
   does not know the size of the pointer to the variable/object (a few bytes), so   cannot be referenced correctly;

5.void Pointer cannot participate in pointer operations unless the conversion is  
   (type*) vp++; 
  //vp==vp+sizeof (type)

The difference between void * and void in the return value of a function

A problem that is easily confused.

In the return value of a function, void is no return value, and void * is a pointer to a value of any type.

Let's look at the code:

#include <stdlib.h>
#include <stdio.h>

void voidc (int a);
void* VOIDCP (int *a);
int main () {
int a=10;
int *ap;

VOIDC (a);
AP = VOIDCP (&a);
printf ("%d\n", *ap);

return 0;
}
void voidc (int a) {
printf ("%d\n", a);
Return No return value
}
void* VOIDCP (int *a) {
printf ("%d\n", *a);
return A; return int *
}

The result is:

10

10

10

Reprint--void pointer (void * usage)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.