O
O
OccamaRazor2016-11-04 20:19:52
Programming
OccamaRazor, 2016-11-04 20:19:52

How to correctly pass multiple values ​​to a function?

C language.
Now it shows only the last variable max , that is, it displays 100 ignoring the rest of the data and the array. Can you tell me how to implement?

#include <stdio.h>

int binarySearch(int key, int array[],int max, int min);

int main(void)
{
  int array[10] = {1,2,3,4,5,6,7,8,9};
  int result = binarySearch(23, array, 0, 100);
  printf("RESULT: %d \n", result);
  system("pause");
}

int binarySearch(int key, int array[], int max, int min)
{
  return (key, array, max, min);
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Moseychuk, 2016-11-04
@fshp

Declare a struct and return it

1
15432, 2016-11-04
@15432

This construction performs all the actions specified with a comma and returns the result of the last one.
return (key, array, max, min);
So it will return the min value (which is 100 because you're passing 100 to the min argument, which is weird too). It's not very clear what you wanted to do here. So that the compiler itself understands that you need to look for a value in an array? It doesn't work that way, you have to write your own search.

A
abcd0x00, 2016-11-05
@abcd0x00

The "comma" operation is a binary operation that is performed from left to right. The left operand is evaluated first, then the right operand is evaluated. After that, the computed value of the right operand becomes the result of the operation.
These are three "comma" operations.
It is done like this
In order not to confuse the "comma" operation with initialization lists, it is customary to put a space before the comma in the "comma" operation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question