整数数组作为Structure变量传递

时间:2014-05-05 20:16:45

标签: 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;
}

这可能是它,但作为一个新手,我无法想象这件事

1 个答案:

答案 0 :(得分:-2)

数组大小的原因是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