Null pointer and NULL

#include <stdio.h> 
int the Add (A int, int B) { 
    return A + B; 
} 
void MAIN1 () { 
    // definition of function pointers trilogy 
   int add (int a, int b ); // first step: function declaration 
    int (* p) (int a , int b); // second step: the function name to the (P *) () greater than the priority * 
    // simplified int (* p) (int, int); definition of a function pointer variable p, the function for pointing to the first address of the return value of type int, int (*) (int int ) plus p is a pointer to the name 
    // Comparative int * p (int a, int B); but it is not a definition of the function pointer declared function, i.e. a return value declared type of function pointer type. 
    p = add; // third step: the function name of the function representing the first address pointer initialized 
    int res = p (1,3); // Step 4: calling the function pointer indirectly 
    printf ( "% d \ n" , res ); 
    // function is the code, the code will change, p ++ meaningless 
    void * pAdd = add; specified function does not return when the pointer variable Padd // define the type and parameter list, not with pAdd (1,3) to call; 
    // defining a function pointer, the function's parameter list and return type indispensable 
}

////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////////////////////// 
void MAIN2 () { 
    // int * P = 0x56777; directly address assigned to the pointer variate, p pointer is stored in the address 0x56777, which does not know what type according to resolve, it is not legitimate 
    int * pInt = (int *) 0x56777; // the address 0x56777 cast to type int *, pInt knew from 0x56777 start address according to an int analysis data of 

    int NUM = 10; 
    Double D = 10.4; 
    int * P1 = # 
    Double * P2 = & D; 
    void * pVoid = P1; 
    pVoid = P2; // void pointers primarily to pass the address 
   // printf ( "% d \ n ", * pVoid); // void pointer address stored only p2 and p2 is not the type of storage, it can not be removed with his * value 
    printf ( "% f \ n ", * ((double * ) pVoid)); // void pointer simply store the address p2, cast to type double *, (double *) pVoid, know by a double resolving  
    // null pointer for parameters or return values, without explicit pointer type, the delivery address
    // null pointer should for a data type, the need for cast
 
    // null pointer or a pointer can point to any data type address, you do not need to cast, only if you want to use 
    // NULL is not point anywhere, mainly for condition determination 
    int * pInt1 = NULL; // null pointer pointing 
    IF (pInt1 == NULL) { 
        the printf ( "pInt1 point without any address"); 
    } 
} 

//// ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////////////////// 
#include <memory.h> // use memset function 
void main3 () { 
    char STR [30] = "China Great IS"; 
    int NUM [. 5] = {1,2,3,4,5}; 
    Memset (STR, 'A',. 5); // void * __cdecl Memset (void * _Dst, int _Val, size_t _Size); str Dst null pointer address passed from here onward 5 bytes byte, are assigned to the characters 'a' 
    the printf ( "% S \ n-", STR); // iS AAAAA great
    memset (num, 0,20); // num is the first address to the Dst null pointer, start here next 20 bytes are assigned to the characters 0, it clears the whole array 
    int * to as pint = (int *) pVoid; 
    for (int I = 0; I <. 5; I ++) { 
        to as pint [I] = I; 
    for (int i = 0; i <5;++i) {
        the printf ( "% D,", NUM [I]); // 0,0,0,0,0, 
    } 
} 


////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///////////// 
#include <stdlib.h> 
void main () { 
    // define a pointer type information contains three 
    // 1, the first address 2, steps (int float double), 3, how to interpret the content (% D,% F) 
    // the malloc (1024 * 1024 * 100) is assigned brackets 100M bytes 
    void * pVoid; 
    pVoid = the malloc (20 is); // allocated 20 words memory section, this memory address pVoid first save space 
    // as the first address, the same steps, the following analytical data of 20 bytes of memory in two ways 
    @% d parsing according 
        printf ( "% d,% f || ", to as pint [I], to as pint [I]); //0,0.000000 2,0.000000 || || || 1,0.000000 4,0.000000 3,0.000000 || || 
    } 
    the printf (" \ n-\ n- "); 
    //% f in accordance with resolution,
    void *pVoidF = malloc(20);
    float *pFloat = (float *)pVoidF;
    printf("%d\n\n", sizeof(float *));
    for (int i = 0; i < 5; ++i) {
        pFloat[i]=i;

        printf("%d,%f,%d||",pFloat[i],pFloat[i],*pFloat);  //0,0.000000,0||0,0.000000,1072693248||0,0.000000,1073741824||0,0.000000,1074266112||0,0.000000,1074790400|| 没有搞懂
    }

}

 

Guess you like

Origin www.cnblogs.com/luoxuw/p/11313411.html