-2

I've written a little program to determine the minimum element in an array a[]. When I debug, the program seems to be working fine initially but then the for loop in the min() function stops execution after two steps, despite the array being of size 10.

The final output is 23, when it sould be 3.

Is the code sizeof(a)/sizeof(int) incorrect? I found it on another article on stackoverflow.

#include <stdio.h>
#include <limits.h>

int min(int[]);

int main(){
    int a[]={100,23,3,4,5,6,7,8,9,10};
    printf("%d", min(a));
    return 0;
}

int min(int a[]){
    int min = INT_MAX, i;
    for(i = 0; i < sizeof(a)/sizeof(a[0]); i++){
        if((i>=0) && (a[i]< min))
            min = a[i];
    }
    return min;
}
Community
  • 1
  • 1
Luke Collins
  • 1,433
  • 3
  • 18
  • 36
  • 2
    When you pass an array to a function, it decays to a pointer. Doing `sizeof` on a pointer only gives you the size of the pointer and not what it points to. – Some programmer dude Feb 03 '16 at 12:07
  • No it's not correct in this case. See the second answer on the question you linked. – user694733 Feb 03 '16 at 12:07
  • @Luke Collons: As Joachim Pileborg mentioned in comment just find size of array using `sizeof(a)/sizeof(a[0])` and pass it to `min` function as second argument and use it in `min` function. – Sagar Patel Feb 03 '16 at 12:18

1 Answers1

1

Inside the function, a is a pointer to integer array, so sizeof(a) returns the size of a pointer which is typically 4 or 8 bytes depending on the system. Saying that, it is not possible to count the number of elements in a C array using sizeof(a)/sizeof(a[0]) inside the function. You need to pass the number of elements explicitly as an argument to the function.

int min(int a[], int n) {
    ...
    for (i = 0; i < n; i++) {...}
    ...
}

printf("%d", min(a, sizeof(a)/sizeof(a[0])));
taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • Downvoted, because this question has already been answered in more depth in other questions. – user694733 Feb 03 '16 at 12:10
  • You say: *a is a pointer to integer array*`, but a` is not a pointer to an integer array. – 2501 Feb 03 '16 at 12:11
  • @user694733, I wrote the answer before seeing the duplicate link. Not sure whether that is enough reason to down vote. – taskinoor Feb 03 '16 at 12:14
  • @2501 what do you mean by `a` is not a pointer? – taskinoor Feb 03 '16 at 12:14
  • That is not what I wrote. – 2501 Feb 03 '16 at 12:14
  • @2501 I didn't get what you mean. It would have been more helpful to me if you described in more depth. – taskinoor Feb 03 '16 at 12:16
  • You wrote: *`a` is a pointer to integer array* , but `a` is not a pointer to an integer array. – 2501 Feb 03 '16 at 12:17
  • @2501 Then what is `a`? – taskinoor Feb 03 '16 at 12:17
  • @user694733 You kind of people are virus in this site – Zain Farooq Feb 03 '16 at 12:35
  • 1
    @ZainFarooq With my voting I try to encourage people in certain direction. Feel free to hate me for it, but please remember that I do not hate this answer; I simply think that closing as a dupe was more appropriate than giving answer. Downvotes should not be taken as a personal insult. – user694733 Feb 03 '16 at 12:41
  • This is not my question or answer but I have experienced you people. You people discourage others to ask questions and make answers here. You have a lot of knowledge but you are arrogant of it and you misuse your voting here. @user694733 – Zain Farooq Feb 03 '16 at 14:21
  • @ZainFarooq As I said, I want to encourage people to read existing answers as they are quite complete already. I don't downvote just to make myself feel better. If you want to discuss this further, create a [chat room](http://stackoverflow.com/help/privileges/chat-rooms) for it. In the mean time, please avoid [name-calling](http://stackoverflow.com/help/be-nice). – user694733 Feb 03 '16 at 14:51