Linux C / C ++ base - two parameter Pointers

1. The two parameter Pointers

  #include<stdio.h>                                                                                          
  #include<stdlib.h>
   
  void Fun ( int ** TEMP)
   {
       * TEMP = ( int *) the malloc ( the sizeof ( int ));
       ** TEMP = 100 ; // can, but before the two variables * not common, this common following 
// int = P * (int *) the malloc (the sizeof (int));
// * P = 100;
// * TEMP = P;
} int main () { int * P = NULL; Fun (& P); the printf ( " % P = D * \ n- " , * P); Free (P);
P = NULL;
return 0 ; }

 

 

 

 2. The value transfer 1

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

void fun(int *tmp)
{
    tmp = (int *)malloc(sizeof(int));
    *tmp = 100;
}

int main(int argc, char *argv[])
{
    int *p = NULL;
    Fun (P); // value passed parameter argument changes do not affect 
    the printf ( " * D P =% \ n- " , P *); // ERR, null pointer memory operation

    return 0;
}

 

 Value is passed 2

  1 #include <stdio.h>                                                                                         
  2 #include <stdlib.h>
  3 
  4 void fun(int *tmp)
  5 {
  6     *tmp = 100;
  7 }
  8 int main(int argc, char *argv[])
  9 {
 10     int *p = NULL;
 11     p=(int *)malloc(sizeof(int));
 12 is      Fun (P); // passed by value, parameter changes do not affect the argument 
 13 is      the printf ( " * D P =% \ n- " , * P);
  14      Free (P);
  15      P = NULL;
  16      return  0 ;
  17 }

 

 

 

 Return address stack area

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

int *fun()
{
    int *tmp = NULL;
    tmp = ( int *) the malloc ( the sizeof ( int ));
     * tmp = 100 ;
     return tmp; // return address stack area, the function call is completed, do not release 
}

int main(int argc, char *argv[])
{
    int *p = NULL;
    p = fun();
    printf("*p = %d\n", *p);//ok

    // heap space, use, manual release 
    IF (the p-! = NULL)
    {
        free(p);
        p = NULL;
    }

    return 0;
}

 

 

 

 

Guess you like

Origin www.cnblogs.com/xiangdongBig1/p/11904162.html