A summary of the second-level pointer initialization as a function parameter transfer

Source: Internet
Author: User

I useCodeTo describe:

Typedef struct {int * Top; int * base; int stack_size;} sqstack; void initstack (sqstack ** s) {* s = (sqstack *) malloc (sizeof (sqstack); If (null! = * S) {(* s)-> Top = (int *) malloc (sizeof (INT); (* s)-> base = (* s) -> top; (* s)-> stack_size = 10 ;}} int main (INT argc, _ tchar * argv []) {sqstack * s = NULL; initstack (& S); If (null! = S) cout <s-> stack_size <Endl; retrun 0 ;}

In the main function, initstack (& S) removes the s address because if the s address is used instead of the s address, in the initstack function, S is equivalent to a local variable, assigning a value to a local variable does not affect the value of S in the main function. Besides, it is not possible to compile the variable using initstack (s. Here, using a second-level pointer is equivalent to passing the pointer address to the form parameter. Then, in the function, perform operations on the s address and change the s value in initstack, at the same time, it will be synchronized in the main function.

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.