Question: The following is in C: Given the code below, please answer the questions (below the code)-- I know there is a lot of code, but

The following is in C:

Given the code below, please answer the questions (below the code)-- I know there is a lot of code, but there are only 4 relevant questions about the code.

The following is in C: Given the code below, please answer the

}

35

36

questions (below the code)-- I know there is a lot of code,

a) The first call to gfind_max is valid, and produces the expected result, however, the following three calls are invalid, producing 0xfffff, 0x44 (or 'D'), and 0x90000 respectively. I understand why 0xfffff is produced, but not the latter two, how does the char comparison reach 0x44 (my initial thought was overflow? But I was unable to decide which value it ultimately caster to be the max char in the first place). The last one compares them as char*s, (I assume to compare the pointers, but I don't know why you would want to compare them since it's just comparing the addresses I assume), why are the last two calls invalid, and why are they producing these results?

b) If you run the code as is--it works. but if you change the code to use cmp_first_char for both sort and search, when will your code crash (and why?)?

(c and d see image below)

but there are only 4 relevant questions about the code. } 35

1 #include 2 #include 4 int cmp_ptr(const void *p, const void q 6 return (const char *)p (const char )q; 9 int cmp_int(const void *p, const void *q) 10 { 11 return *(const int )p*(const int *)q; 12 > 13 14 int cmp_char(const void *p, const void *q) 15 f 16 return *(const unsigned char *)p-*(const unsigned char)q; 17 18 19 int cmp_first_char(const void *p, const void *q) 20 21 return*(const char)p(const char**)q 23 24 int p_a symmetric(const void *p, const void *q) 25 { 26 return *(const char )p**(const char)q; 27 28 29 void test bsearch(chararrl,int n) 30 char ch = 'z'; 32 qsort (arr, n, sizeof (arr[0]), cmp_first_char) 33 char **found bsearch(&ch, arr, n, sizeof(arr[0]), cmp_asymmetric) 34 printf(" %c matches? %s ", ch, found ? *found : "none"); 1 #include 2 #include 4 int cmp_ptr(const void *p, const void q 6 return (const char *)p (const char )q; 9 int cmp_int(const void *p, const void *q) 10 { 11 return *(const int )p*(const int *)q; 12 > 13 14 int cmp_char(const void *p, const void *q) 15 f 16 return *(const unsigned char *)p-*(const unsigned char)q; 17 18 19 int cmp_first_char(const void *p, const void *q) 20 21 return*(const char)p(const char**)q 23 24 int p_a symmetric(const void *p, const void *q) 25 { 26 return *(const char )p**(const char)q; 27 28 29 void test bsearch(chararrl,int n) 30 char ch = 'z'; 32 qsort (arr, n, sizeof (arr[0]), cmp_first_char) 33 char **found bsearch(&ch, arr, n, sizeof(arr[0]), cmp_asymmetric) 34 printf(" %c matches? %s ", ch, found ? *found : "none")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!