▲ 0 r/C_Programming
int chunk_start_commpar(const void* a,const void* b){
const Chunk *a_chunk =a;
const Chunk *b_chunk =b;
return (*a_chunk).start - (*b_chunk).start ;
}
int chunk_list_find (Chunk_List* list ,void* ptr){//we use int as return variable type -1 if nothing found, 1 otherwise
Chunk key ={
.start =ptr
};
Chunk* result = bsearch(&key,list->chunks,list->count,sizeof(list->chunks[0]),chunk_start_commpar
);
return (result - list->chunks) / sizeof(list->chunks[0]) ; } return (result - list->chunks) / sizeof(list->chunks[0]) ;
}
If i were to remove the const from the comapre fucntion the code gives an error:
"passing argument 5 of ‘bsearch’ from incompatible pointer type [-Wincompatible-pointer-
Why is adding const to the inputs even important, we are'nt changing anything about a,b
u/ManagementOpposite61 — 17 days ago