有如下程序 #include <stdio.h> struct pair { int first, second; }; struct pair get_min_max(int* array, int len) { int i; s

admin2021-07-09  22

问题 有如下程序
#include <stdio.h>
struct pair
{
    int first, second;
};
struct pair get_min_max(int* array, int len)
{
    int i;
    struct pair res;
    res.first = array[0];
    res.second = array[0];
    for (i=1; i<len; i++)
    {
        if (array < res.first)
            res.first = array;
        if (array > res.second)
            res.second = array;
    }
    return res;
}
main()
{
    int array[5] = {9, 1, 3, 4};
    struct pair min_max = get_min_max(array, 5);
    printf("min=%d,max=%d\n", min_max.first, min_max.second);
}
程序运行后的输出结果是

选项 A、min=1,max=9
B、min=0,max=9
C、min=1,max=4
D、min=0,max=4

答案B

解析 在对数组进行初始化时如果在说明数组时给出了长度,但没有给所有的元素赋予初始值,而只依次给前面的几个数组元素赋予初值,那么C语言将自动对余下的元素赋初值0,则array[5] = {9, 1, 3, 4,0}。程序的执行过程为:调用函数get_min_max(array,5),将数组array首地址传入函数,定义结构体变量res,并为其成员赋值。for循环查找数组array数组的最小值0,将其赋值给res的成员first,查找数组最大值9,赋值给res的成员second。最后返回结构体变量res,则min_max=res。输出min_max.first=0,min_max.second=9,B选项正确。
转载请注明原文地址:https://kaotiyun.com/show/oEkp777K
0

随机试题
最新回复(0)