c - 作为结构变量传递的整数数组

标签 c sizeof

这是我的代码,不明白为什么数组的大小是2。 帮助我纠正我的代码,以便我能够正确使用 Max() 函数。

//Max() gets the max value and Min()gets min value 

#include <stdio.h>
#include "conio.h"

struct test {
    int input[10];//input array , I need to get out Max and min functions right
    int min;
    int max;
} testDB[2] =
    {
        {{1,2,3,4,5,6},1,6},
        {{3,4,5},3,5},
    };

int Max(int* input){
    int max,i,size;
    max = input[0];
    size = sizeof(input)/sizeof(input[0]);
    printf("\n-----------\n");//print debugging
    printf("\t%d",size);
    printf("\n-----------\n");//print debugging 
    for(i=0;i<size;i++)
        {
            if(max<input[i])
                max = input[i]; 
            printf("%d\n",max);
        }
    return max;
}
int Min(int*input)
{
    int min,i;
    min = input[0];
    for(i=0;i<sizeof(input)/sizeof(input[0]);i++)
        {
            if(min>input[i])
                min = input[i];
        }
    return min;
}

void testCases()
{
    int max,min,i;
    for(i=0; i<2; i++) {
        printf("Test cases for Max\n");
        max = Max(testDB[i].input);
        if((testDB[i].max==max)) printf("PASSED\n"); else printf("FAILED\n");
        printf("TestCases for Min\n");
        min = Min(testDB[i].input);
        if((testDB[i].min==min)) printf("PASSED\n"); else printf("FAILED\n");
    }

} 
int main()
{ 
    testCases();
    return 0;
}

这可能是它,但作为一个新手我无法弄清楚这个东西

最佳答案

数组大小为2的原因是 看看

} testDB[2] =
    {
        {{1,2,3,4,5,6},1,6},
        {{3,4,5},3,5},
    };

在这种情况下,它要么与 ([1,2,3,4,5,6],1,6) 中的一个值一起使用,要么与 ([3,4,5],3,5 )。 因为它只存储 2 位数字,所以数组的大小是 2。就这么简单。

梅根 35:n2KVjLKVmLKukLK7muG+lgK0lHzZlwu5

关于c - 作为结构变量传递的整数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23481027/

相关文章:

c++ - C++模板参数sizeof返回错误的结果

c++ - 在运行时查找数组的大小

c - 如何在 C/C++ 中实现进程调节器?

c - 为什么 unsigned int 包含负数

c - 逐字读取没有结尾字符的文件

c - puts 和 gets 函数如何工作?

有符号/无符号值与负值之间的比较

c - 时间测量总时间与 cpu 时间

c++ - sizeof 继续返回 4 而不是实际大小

c - 传递数组名称时 sizeof 如何工作