Using sort/qsort in C [duplicate] - c

This question already has answers here:
How does the compare function in qsort work?
(6 answers)
Closed 3 years ago.
Is there a 'built-in' sort, in which the int (*compar)(const void *, const void*) does not need to be specified? For example, I find that about 99% of the time I'll do either a basic ascending or descending sort and not care about a special comparison case.
Is there the equivalent of an "in-line" function that can be specified here, or is it always necessary to have a standalone function to do this comparison?

I believe that qsort() is the only "build-in" sorting function in C. But if you write C++, std::sort() is also available.

Related

Any append (Python) or push_back (C++) equivalent in C? [duplicate]

This question already has an answer here:
Python-like array filling - C equivalent
(1 answer)
Closed 2 years ago.
I'm looking for an "append" (Python) or "push_back" (C++) equivalent in C to fill an array of strings (char). Does it exists?
The question doesn't make sense because you cannot add elements to an array in C. In C, you set the size of the array when you create it, and the size cannot change.
If you want to "append", you have to create a big array (e.g. 1000 items), and use a separate variable to remember how many items you're actually using (the rest are spare).

In C++11 when should we explicitly use std::decay? [duplicate]

This question already has answers here:
What is std::decay and when it should be used?
(2 answers)
Closed 6 years ago.
We know that compiler will decay array/function to pointers when needed(according to context), then when is the time we should explicitly use std::decay?
Is there any task that requires us to use it, or else, compiler doesn't know how to handle it?
Thanks.
Simply put, decay::type is the identity type-transformation except if T is an array type or a reference to a function type. In those cases the decay::type yields a pointer or a pointer to a function, respectively.
For more detail please see this https://stackoverflow.com/a/25732651/1691223

When should I use pass by reference instead of pass by value? [duplicate]

This question already has answers here:
When should I pass or return a struct by value?
(9 answers)
Closed 7 years ago.
I know the difference between pass by value and pass by reference. I use them and understand how they work in the codes that I've dealt so far. However, I'm looking for a general rule. What is generally the best time to use pointers and what is the best to use actual values? Examples are much appreciated.
As a general rule, pass-by-value for basic types (int, char, etc.), and pass-by-pointer (or better, pass-by-reference) for big data as struct.
Thinking of a struct with 1000 data members, and the cost to copy that gigantic data to a function. It'd be much quicker to pass-by-pointer or pass-by-reference in that case.

Is `qsort()` really QuickSort? [duplicate]

This question already has answers here:
Quicksort implementation in C?
(1 answer)
What sorting algorithm does qsort use?
(3 answers)
Closed 9 years ago.
Silly question perhaps, but is there any guarantee that the qsort() routine in the C standard library really implements the QuickSort algorithm?
Since there is nothing in the standard that nominates QuickSort specifically, no.

Concept of function pointers in C? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the point of function pointers?
hi all,
I want to get the basic and concrete idea of function pointers in C language.
ie 1) its usage in C
2) main applications it is currently using
3) unique features
4) its scope in embedded applciations etc
Hoping your co operation in this too.
__Kanu
Function Pointers are pointers, that is variables, which point to the address of a function.
Nice example here. Also this answer is a must read.

Resources