C语言--跨函数使用内存

#include<stdio.h>
#include<malloc.h>

typedef struct {
    int pi;
    int pd;
    int id;
}PID;

PID *Create_Fun(void);
void Fun_Show(PID *ps);

int
main(int argc, char *argv[])
{
    PID *ps;

    ps = Create_Fun();
    Fun_Show(ps);
    free(ps);
    Fun_Show(ps);

    return 0;
}

PID *
Create_Fun(void)
{
    PID *pointer = (PID *)malloc(sizeof(PID));

    if (pointer != NULL) {

        pointer->pi = 23;
        pointer->pd = 53;
        pointer->id = 99;

        return pointer;
    } else
        exit(-1);

}


void
Fun_Show(PID *ps)
{
    printf("%d %d %d \r\n", ps->pi, ps->pd, ps->id);
}

#if 0
程序输出结果:
23 53 99
6960544 6950896 99
#endif // 0

猜你喜欢

转载自blog.csdn.net/tyustli/article/details/86546094
今日推荐