l2--using memory across functions

Source: Internet
Author: User

Introduced

#include <stdio.h>
#include <malloc.h>intMainvoid){ int*p;//defines a pointer variable, but does not have a period of initial value; The following two functions are for their FU initial value operationf (&p);//The f function, the pointer variable p is attached with an exact value, so it is the correct operation//g (&p);//g function, although the G function does not have an exact value when it is executed, but the value of the pointer variable after the execution of the G function is not an exact value return 0;}voidFint**q) { *q = (int*) malloc (4);//dynamically allocated memory, the operating system does not automatically release memory for programmers without manually releasing it}voidGint**q) { intR//statically allocated memory, after the function (g) is called, the operating system automatically releases memory (out of the stack)*q = &R;}
//To implement the use of memory across functions, only through the use of dynamic memory methods
Example
#include <stdio.h>#include<malloc.h>structstudent{intSID; intAge ;};structStudent * Createstudent (void);voidShowstudent (structStudent *);intMainvoid){    structStudent *PS; PS=createstudent ();    Showstudent (PS); return 0;}voidShowstudent (structStudent *PST) {printf ("%d%d\n", Pst->sid, pst->Age );}structStudent * Createstudent (void) {    structStudent * p = (structStudent *) malloc (sizeof(structStudent)); P->sid = About; P->age = the; returnp;}/*Output Result:*/

l2--using memory across functions

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.