13177531678 发表于 2019-3-28 07:53:19

函数为什么没起到作用?

//题目是输入四个数用顺序结构排序//



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

void main()
{
        int swap(int a, int b);
        int a, b, c, d;
        printf("pleause input a,b,c,d intuen:");
        scanf_s("%d%d%d%d",&a,&b,&c,&d);
        if (a < b)
        {
                swap(a, b);
        }
        if (a < c)
        {
                swap(a, c);
        }
        if (a < d)
        {
                swap(a, d);
        }
        if (b < c)
        {
                swap(b, c);
        }
        if (b < d)
        {
                swap(b, d);
        }
        if (c < d)
        {
                swap(c, d);
        }
        printf("a,b,c,d从大到小的排序位%d %d %d %d", a, b, c, d);
        system("pause");
}

int swap(int a, int b)
{
        int t;
        t = a;
        a = b;
        b = t;
}

wp231957 发表于 2019-3-28 08:19:10

交换函数 必用指针这是常识了

BngThea 发表于 2019-3-28 08:43:59

因为直接传值是没有用的,在函数中进行的替换都只在函数中有效
可以用指针或引用来实现
页: [1]
查看完整版本: 函数为什么没起到作用?