0

Here is my code, cant figure out why the size of array is 2. Help me out in correcting my code, so that i can get the Max() function right.

//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;
}

This is probably it , But as a newbie I am not able to figure this thing

Zong
  • 6,160
  • 5
  • 32
  • 46
  • 4
    `input` is a pointer, not an array. So `sizeof(input)` is the size of a pointer, not the size of the array that it points to. – Barmar May 05 '14 at 20:21
  • 1
    As @Barmar said (in the function, `input` is a pointer, not an array). That's why you normally pass the number of elements in the array to the function as a separate argument. `int Max(int *input, int num)` would be the classic interface; with C99, you might be better off using: `int Max(int N, int input[N])` with the parameters in the reverse order (because N must be defined before it can be used as an array bound). (OTOH: since you `#include "conio.h"`, you probably don't have C99 support yet.) – Jonathan Leffler May 05 '14 at 20:23

1 Answers1

-2

The reason for size of array is 2 is look at the

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

in this case, either it goes with one value from ([1,2,3,4,5,6],1,6) or it goes with the ([3,4,5],3,5). So as it store only 2 digit the size of array is 2. as simple as that.

Megan 35 : n2KVjLKVmLKukLK7muG+lgK0lHzZlwu5

Michael
  • 6,451
  • 5
  • 31
  • 53
Vandan
  • 22
  • 3
  • 1
    I think you'll find the OP is building on a 64-bit machine with `sizeof(int) == 4` and `sizeof(int *) == 8`, giving the result 2. On a 32-bit machine, the answer would be 1 (because `sizeof(int *) == 4` on a normal 32-bit machine). – Jonathan Leffler May 05 '14 at 20:25
  • The code does not compute the size of an array. Even if it did, the result would not be 2, because the sizeof operator does not compute the number of elements. (What size would it report for testDB if an int has 32 bits? What if an int has 64?) – Peter - Reinstate Monica May 05 '14 at 20:32
  • -1, the answer does not make sence. I suspect it is wrong, but can not tell for sure. – ctrl-alt-delor May 05 '14 at 20:44