Home > Enterprise >  Returning value from a function with 'NULL' as parameter
Returning value from a function with 'NULL' as parameter

Time:11-01

I'm fairly new in C, and I've got an assignment in which functions return MIN and MAX value of elements in an array. But I also have to return value from function with 'NULL' parameter and I don't understand it.

My code:

int array_min(const int [], const int);
int array_max(const int [], const int);

int main() {

    int input_arr[] = {2,3,4,5,6};
    printf("%d\t", array_min(input_arr, 5));
    // : 2
    printf("%d\t", array_max(input_arr, 5));
    // : 6
    printf("%d\t", array_max(NULL, 5));
    // : -1

    return 0;
}

int array_min(const int input_arr[], const int arr_size) { 
    int a, b = arr_size, min = input_arr[0];
    for(a = 0; a < b; a  ) {
        if(input_arr[a] < min) {
            min = input_arr[a];
        }
    }
    return min;
}

int array_max(const int input_arr[], const int arr_size) { 
    int a, b = arr_size, max = input_arr[0];
    for(a = 0; a < b; a  ) {
        if(input_arr[a] > max) {
            max = input_arr[a];
        }
    }
    return max;
}

So, my question is, how do I use 'NULL' in function to get return value -1?

Thanks for all the aswers. I greatly appreciate them.

CodePudding user response:

It is modified version of the @chux-ReinstateMonica answer so please upvote his answer (and DV tis one) with some changes.

  1. Use size_t (not int) for sizes.
  2. Try if possible to have one return point from the function
  3. There is no need to check index 0 as it will be equal to the min value
  4. In this case it is better to keep value and the index of the minimal value instead of pointer to it (it will help the compiler to optimize the code)
const int *array_min1(const size_t arr_size, const int input_arr[arr_size]) { 
    const int *result = arr_size ? input_arr : NULL;
    if (result) 
    {
        size_t a, min_index = 0;
        int min = input_arr[0];
        for(a = 1; a < arr_size; a  ) {
            if(input_arr[a] < min) {
                min = input_arr[a];
                min_index = a;
            }
        }
        result = input_arr   min_index;
    }
    return result;
}

CodePudding user response:

how do I use 'NULL' in function to get return value -1?

Parameter input validation is a common task. Simple test up front.

When input is troublesome:

int array_min(const int input_arr[], const int arr_size) { 
  if (input_arr == NULL || arr_size <= 0) {
    return -1;
  }
  ...

Returning -1 on error is a weak design choice as it is not distinguishable from a good return of -1:

int input_arr[] = {-1, 2, 3, 4, 5};
printf("%d\t", array_min(input_arr, 5));

functions return MIN and MAX value of elements in an array.

Consider instead: return the address of the minimum/maximum. Return NULL on error.

// int array_min(const int input_arr[], const int arr_size) { 
const int *array_min(const int input_arr[], const int arr_size) { 
  if (input_arr == NULL || arr_size <= 0) {
    return NULL;
  }
  //int a, b = arr_size, min = input_arr[0];
  int a, b = arr_size;
  const char *min = &input_arr[0];
    // for(a = 0; a < b; a  ) {
    for(a = 1; a < b; a  ) {  // No need to test input_arr[0]
        //if(input_arr[a] < min) {
        if(input_arr[a] < *min) {
            //min = input_arr[a];
            min = &input_arr[a];
        }
    }
    return min;
}

Now the calling code can detect troubles.

// printf("%d\t", array_min(input_arr, 5));
const int *min = array_min(input_arr, 5);
// or 
const int *min = array_min(input_arr, 0);
// or 
const int *min = array_min(NULL, 5);
if (min) {
  printf("%d\t", *min);
} else {
  printf("Min not found\t");
}
  • Related