c returns multiple values

c training
1. Utilize global variables (poison)

#include <stdio.h>
//input five int numbers,return min & max, in one function
int min_element;
int max_element;

int min_max(int a,int b,int c)
{
    int t;
    if (a > b) {
        t = a;
        a = b;
        b = t;
    }
    if (a > c) {
        t = a;
        a = c;
        c = t;
    }
    if (b > c) {
        t = b;
        b = c;
        c = t;
    }
    min_element = a;
    max_element = c;
}

int main ()
{
    int a,b,c,d,e;

    scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
    min_max(a,b,c);
    min_max(min_element,max_element,d);
    min_max(min_element,max_element,e);

    printf("%d %d \n",min_element,max_element);
    return 0;
}
#include <stdio.h>
//int a = 3 ; float b = 4.0; double c ,c = a + b; double d , d = a/b; function 1
double c,d;

double function_1(int a, float b)
{
    c = a + b;
    d = a/b;
}

int main ()
{
    int a = 3;
    float b = 4.0;
    function_1(a,b);
    printf("double c = %f double d = %f \n",c,d);
    return 0;
}

2. Pass the array pointer

**#include <stdio.h>
//利用数组指针传递
void min_max(int *ptr,int n)
{
    int *template;
    int i;
    for (i = 0; i < 5; i++)
    {
        if (*ptr > *(ptr + i))
        {
            *template = *(ptr + i);
            *(ptr + i) = *ptr;
            *ptr = *template;
        }
        if (*(ptr + n - 1) < *(ptr + i))
        {
            *template = *(ptr + i);
            *(ptr + i) = *(ptr + n - 1);
            *(ptr + n - 1) = *(ptr + i);
        }
    }
}

int main ()
{
    int a[5];
    int i;

    for (i = 0; i < 5; i++)
        scanf("%d", &a[i]);
    min_max(a, 5);

    printf("%d %d", a[0], a[4]);
    return 0;
}

3. Pass the structure pointer

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325879028&siteId=291194637