Chayan
Chayan

Reputation: 21

Can't find where is the error , but showing error in this C program

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.

Upvotes: -2

Views: 76

Answers (1)

user9706
user9706

Reputation:

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;
}

Upvotes: 1

Related Questions