Segmentationfault (linuxc) -- Linux general technology-Linux technology and application information. The following is a detailed description. No more than 20 int-type data are read from the keyboard to form an array. The qsort function is used to sort the data and then an integer number is read from the keyboard. The binary method is used for searching.
I have compiled the following functions, but segmentation fault appears during the invisibility.
# Define m 20
# Include
# Include
Int compar (const void * a, const void * B)
{
Int * aa = (int *) a, * bb = (int *) B;
If (* aa> * bb) return 1;
If (* aa = * bb) return 0;
If (* aa <* bb) return-1;
}
Main ()
{
Int base [m], n;
Int I, * key, * itemptr;
Printf ("input n (n <20) \ n ");
Scanf ("% d", & n );
For (I = 0; I {
Scanf ("% d", & base
); }
Qsort (base, n, sizeof (int), compar ); For (I = 0; I { Printf ("% d", base); } Printf ("\ n "); Scanf ("% d", key ); Itemptr = (int *) bsearch (key, base, 20, sizeof (int), compar ); If (itemptr! = NULL) { Printf ("% d is in the table! \ N ", * key ); } Else { Printf ("% d isn' t in the table! \ N ", * key ); } }
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service