C + + Pointer II (Error-prone model)

Source: Internet
Author: User

Rule one:the memory allocated by Main (the core function) can be used in the called function, either in the heap, the stack, or the global zone. If you allocate memory in the staging area (stack) inside the called function, the primary call function is not available.

#include"stdio.h"#include"stdlib.h"#include"string.h"Char*getstring1 () {Char*P1 ="ABCDE"; returnp1;}Char*getstring2 () {Char*P2 ="ABCDE"; returnp2;}Char*Getstring3 () {Charbuf[ -]; strcpy (BUF,"ABCDE"); returnbuf;}voidMain () {intI=0; //The pointer refers to the person whose address is assigned to the pointer variable.     Char*P1 =getstring1 (); Char*P2 =getstring2 (); CharP3 = NULL;//P3 is a variable .    Char*P4 =getstring3 ();//printf ("p1:%s\n", p1);//printf ("p2:%s\n", p2);// //printf ("p1:%d\n", p1);//printf ("p2:%d\n", p2);printf ("p4:%s\n", p4); //A pointer variable and the memory space variable it executes are two different conceptsstrcmp (P1, p2); System ("Pause");}

Rule number two: indirect assignment, pay attention to memory conflicts.

#include"stdio.h"#include"string.h"#include"stdlib.h"//See the first-level pointer, to distinguish the input and output characteristics of the pointer//input characteristics of pointers: allocating memory in the main function, using in the called function//output characteristics of pointers: allocating memory within the called function, mainly by throwing out the results of the operationintGetLen3 (int*p/* in Out*/){    //* (NULL) = 40; //* (0) = +;*p = +;//Indirect Assignment If p is the address of a, then indirectly modifies the value of a//*p parameter, to indirectly modify the value of an argument}voidMain () {intA =Ten;//defines a variable (argument)//A as a level 0 pointer    int* p = NULL;////defines a variable (formal parameter)//1 level pointerA = -;//Modify the value of a directlyprintf"a:%d \ n", a); //p = &a;//the address of A is assigned to P//pass a variable to the address to another variable//The actual parameter takes the address to the formal parameterGetLen3 (P);        GetLen3 (NULL); printf ("a:%d \ n", a); System ("Pause");} 
* (NULL) = 40;//* (0)  = 40;
GetLen3 (NULL);
Different compilers will have different prompts. The memory space with zero address is returned to the system. So, the program modifies the contents of the inside, error.

C + + Pointer II (Error-prone model)

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.