-2

While compiling the code the error is showing is conflicting type for a function I have decelerated in this code , but I can't understand where is the error.. Need help to fins the error , and how to fix it.

This a program to sort a array , name of the algorithm is selection sort.

Code

#include<stdio.h>
#include<stdlib.h>

int select(int* arr, int i, int n) {
  // code here such that selectionSort() sorts arr[]

  int min_index = i, j;

  for (j = i + 1; j < n; j++)
  {
    if (arr[min_index] > arr[j])
    {
      min_index = j;
    }
  }

  return min_index;
}

void selectionSort(int arr[], int n) {
  int i, selected_index, temp;

  for (i = 0; i < n - 1; i++)
  {
    selected_index = select(arr, i, n);
    // Swap the selected and posistioned index.
    temp = arr[i];
    arr[i] = arr[selected_index];
    arr[selected_index] = temp;
  }
}

void printSorted_array(int arr[], int size)
{
  for (int i = 0; i < size; i++)
    printf("%d ", arr[i]);
}

int main()
{
  int size, * arr, i;
  scanf("%d", &size);
  arr = (int*)malloc(size * sizeof(int));

  for (i = 0; i < size; i++)
  {
    scanf("%d", &arr[i]);
  }

  selectionSort(arr, size);
  printSorted_array(arr, size);

  return 0;
}

Error

Compilation error

main.c:3:5: error: conflicting types for ‘select’

    3 | int select(int *arr, int i, int n){
      |     ^~~~~~

In file included from /usr/include/x86_64-linux-

gnu/sys/types.h:179,
                 from /usr/include/stdlib.h:394,
                 from main.c:2:

/usr/include/x86_64-linux-gnu/sys/select.h:101:12: note: previous declaration of ‘select’ was here

  101 | extern int select (int __nfds, fd_set *__restrict __readfds,
      |  
      ^~

This is the error and above is the code.

4
  • what is the error? you can't just paste a bunch of code here and expect people to solve your problem. Try to improve your question. Commented Nov 19, 2022 at 8:08
  • Please learn how to format your code properly and how to format a question. It's not hard and it will only take you a couple of minutes. Also read this : How to Ask. You can edit your question in order to improve it. Commented Nov 19, 2022 at 8:10
  • yes sorry for that I just mistakenly press the submit button before paste the error. Commented Nov 19, 2022 at 8:12
  • 4
    Looks like you tried to name your function the same as one from a header you included. Try naming your function "select" something different. Commented Nov 19, 2022 at 8:13

1 Answer 1

1

On Linux select() is already defined in types.h which is included by stdlib.h. Use a different name, for example, selectMin() and update the callers to use the new name, too:

#include <stdio.h>
#include <stdlib.h>

int selectMin(int* arr, int i, int n) {
  // code here such that selectionSort() sorts arr[]

  int min_index = i, j;

  for (j = i + 1; j < n; j++)
  {
    if (arr[min_index] > arr[j])
    {
      min_index = j;
    }
  }

  return min_index;
}

void selectionSort(int arr[], int n) {
  int i, selected_index, temp;

  for (i = 0; i < n - 1; i++)
  {
    selected_index = selectMin(arr, i, n);
    // Swap the selected and posistioned index.
    temp = arr[i];
    arr[i] = arr[selected_index];
    arr[selected_index] = temp;
  }
}

void printSorted_array(int arr[], int size)
{
  for (int i = 0; i < size; i++)
    printf("%d ", arr[i]);
}

int main()
{
  int size, * arr, i;
  scanf("%d", &size);
  arr = (int*)malloc(size * sizeof(int));

  for (i = 0; i < size; i++)
  {
    scanf("%d", &arr[i]);
  }

  selectionSort(arr, size);
  printSorted_array(arr, size);

  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.