C Language Pointer Advanced section: void pointers and data pointers

Source: Internet
Author: User
Conceptvoid pointer data pointer void Pointer

Basic concepts of void pointers

void means "no type", and void pointer is "No type pointer", and a void pointer can point to any type of data.
so void pointers are generally referred to as universal pointers or generic pointers, or universal pointers.

Definition form:
void *p;

* * in C language you can use a void type pointer at any time in place of another type of pointer, and a void pointer can point to a variable of any data type *
* * * * * * * * * If you want to get the variable value that it points to by using the void pointer, You need to convert the void pointer coercion type to a data type pointer that you want to match with the variable name type; * *
the Strong class type of the pointer transforms:
    void  *p;
    int *PA = (int *) p;
    And then you can manipulate the space that the original void pointer points to.
any type of pointer can be assigned to a void pointer without coercion type conversion;
    float f = 1.22f;
    float *P1 = &f;
    p = p1;//assigns float pointer to P

void pointer Applications:
a void pointer can be used when a pure memory operation is performed, or when a pointer to an unspecified type is passed;
void* memcpy (void *addr1,void *addr2,size_t N);
This function only makes a pure memory copy, and any content, including null characters, will be copied

1: Limit the function parameter
2: The qualified 3:void pointer returned to the function
is also used as a function pointer

The code looks like this:

#include <stdio.h>

int main (void)
{
    int num1 = 1;
    Char char1 = ' B ';

    /* Defines two void pointer types *
    /void *void_pointer_1;
    void *void_pointer_2;

    /* give different void pointers to different data types * *
    void_pointer_1 = &num1;
    void_pointer_2 = &char1;

    printf ("void_pointer_1:%d,void_pointer_2:%c\n", * (int *) void_pointer_1), * ((char *) void_pointer_2));

    void_pointer_2 = &num1;
    printf ("void_pointer1:%d\n", * (int *) (void_pointer_2));

    float float_1 = 1.25f;
    float* Float_pointer = &float_1;
    void* Void_float_pointer;
    /** assigns a float type pointer to a void type pointer
    /void_float_pointer = Float_pointer;

    printf ("float_pointer:%f\n", * ((float *) void_float_pointer);

    return 0;
}
Data Pointers

The concept of a data pointer:
In embedded programming, it is possible to read and write content in a specific memory, the assembly has the corresponding MOV command, and in addition to C + + programming language is basically no direct access to memory capabilities
Use of data pointers to manipulate specific memory directly through memory address

For example: in the address 0xff00ff00 corresponding memory wish to write one
unsigned int *p = (unsigned int *) 0xff00ff00;
*p = 11;

The data pointer must be cautious when manipulating specific memory, not all memory can be manipulated, you must have a better understanding of the hardware before you can operate (there may be a segment error)

Code can run, if necessary, you can directly pull down to run a bit, see how the structure of the array is initialized operation and use. Thank you for your visit, if you have written a bad place, I hope you can put forward in time, thank you for watching

Related Article

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.