-3
1.  int minimum(int arr[]){
2.
3.     int min,pos = 0;
4.     min =  arr[0];
5.     int i;
6.     for(i = 0;i<(sizeof(arr)/sizeof(*arr));i++){
7.         if(arr[i]<min){
8.             min = arr[i];
9.             pos = i;
10.        }
11.     }
12.     return arr[pos];
13.  }

in the 6th line it the satement in the for loop condition cant count the size of the array .......

3
  • 3
    Short answer: You cannot. Commented Feb 2, 2019 at 11:21
  • 1
    Are you using C or C++? If it's C++, then std::array<> is what you're looking for. Regular arrays are dumb as they do not know they're size (easily). Commented Feb 2, 2019 at 11:22
  • 1
    Here's some indormation how you can format your code properly when posting at this site. Commented Feb 2, 2019 at 11:24

1 Answer 1

0
int minimum(int arr[])

the number of elements is unknown at compile time, sizeof(arr) cannot values the right value, it will values sizeof(int *) because this is the type of arr

But in

void f()
{
   int arr[3];

   printf("%d\n", sizeof(arr)/sizeof(*arr));
}

the sizeof of arr is known at compile time

Sign up to request clarification or add additional context in comments.

2 Comments

In int minimum(int arr[42]) the number of elements is still unknown by the compiler.
@WeatherVane yes, I wasn't enough clear, I edit, thank you for the advice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.