Mistakes in passing Parameters Using pointers

Source: Internet
Author: User

(I am currently in the cainiao stage ......)

When I create a function today, I want to return a class and an integer. Naturally, I want to use a pointer to return a class, and return to return an integer.

Then my statement is:

Function body:

 
Int funtion (element * P) {element * A = new element ();...... // Some operations on a p = A; // here, P has the value int num = 0; return num ;}

Call function:

 
Element * P = NULL; funtion (p); // The returned P is null.

Then, I explained and compiled a simple example as follows:ProgramThen, I understand!

 
Void funtion (int * A) {A [0] = 5;} int main (INT argc, _ tchar * argv []) {int * B = new int [1]; funtion (B); cout <"* B =" <* B; while (true); Return 0 ;}

Pointers are the same as common parameters. They are all form parameters. They all copy the values of real parameters to the form parameters. The only difference is the nature of the pointer.

When the function parameter is a pointer, the function body uses a passed pointer to operate the object to which the Pointer Points, without changing the value, so that the Pointer Points to other memory.

Just like I made a mistake, when the pointer of the real parameter is originally empty, it is still empty! No matter what operation is performed in the function body or the object to which the Pointer Points, the value of the pointer of the form parameter is changed.

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.