首页 > 其他 > 详细

指针示例----01

时间:2019-11-12 10:44:13      阅读:76      评论:0      收藏:0      [点我收藏+]

今天在论坛看见关于指针使用的讨论,原帖:https://bbs.csdn.net/topics/394991693

我整理一下他们所说的几种方法,可能涉及到内存泄漏

case 1:

#include <stdlib.h>
#include <stdio.h>

int *min(int x, int y)
{
    int *temp = (int*)malloc(sizeof(int));
    *temp =  x < y ? x : y;
    return temp;
}
int main()
{
    int a = 3;
    int b = 5;
    int *c = min(a, b);
    printf("%d\n", *c);
    free(c);
    system("pause");
    return 0;
}

case 2:

#include <stdlib.h>
#include <stdio.h>
    
int *min(int x, int y)
{
    int *temp = (int*)malloc(sizeof(int));
    temp =  x < y ? &x : &y;
    return temp;
}

int main()
{
    int a = 3;
    int b = 5;
    int *c = min(a, b);
    printf("%d\n", *c);

    system("pause");
    return 0;
}

case 3:

#include <stdlib.h>
#include <stdio.h>

int *min(int *x, int *y)
{
    return *x < *y ? x : y;
}
    
int main()
{
    int a = 3;
    int b = 5;
    int *c = min(&a, &b);
    printf("%d\n", *c);

    system("pause");
    return 0;
}

 

指针示例----01

原文:https://www.cnblogs.com/strive-sun/p/11839825.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!