C + + Learning notes (ii)-pointers

Source: Internet
Author: User

1. Pointers to arrays
int balance[ };  int *//ptr is a pointer to the array balance // or it is also possible: ptr = &balance[];      

The difference between balance and PTR is that PTR is a variable and balance is a constant:

 // This is not possible, the IDE will error that the expression must be a modifiable lvalue 
2. Pointer as function return value

According to rookie tutorial: http://www.runoob.com/cplusplus/cpp-return-pointer-from-functions.html

"C + + does not support the return of the address of a local variable outside of a function unless the local variable is defined as a static variable"

Chestnuts originally used random number generation, feel the time to see chaos, directly changed to 1~10 increment, long like this:

// function to generate and return a random number int * getrandom () {  staticint  r[];     for (int0; + +i)  {    = i;     << R[i] << endl;  }    return R;}

In fact, it is well understood that if the variable is not defined as static, the variable in the function is a local variable, the function is called when the local variables allocated memory space, the function returned after the allocated space has been recycled by the operating system.

If the static modifier is removed, the output is garbled, but it can be defined as a global variable:

C + + Learning Note (ii)-pointers

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.