In C, We can assign address of any type to a void pointer without typecasting. Also the address stored in void pointer can be assigned to typed pointer. In C++ and in Turbo C, we require typecasting when we assign void pointer to typed pointer. To make it work, try this strcmp((char *)pa, (char *)pb);
Professor, I need to solve an array of pointers using qsort. I don't have enough time and I need to submit these projects within 2 weeks in my school. I honestly need help.
Professor I am trying to qsort using name but its not happening. int cmpname(const void * pa, const void * pb ){ const card_t *p1 = pa; const card_t *p2 = pb; // return p1->name - p2->name; return strcmp(p1->name, p2->name); } int n = sizeof(cards) / sizeof(card_t); qsort(cards, n, sizeof(card_t), cmpname);
Tried same thing but I kept getting id in something not a structure or union, mycode: /* QSORT and bsearch that returns a 24-bit color value using * built in library function */ #include #include #include //declare color comparism int ColorCompare(const void *pa, const void *pb); //define struct for holding RGB color data typedef struct _RGB { int id; char color[16]; int value; }RGB_t; int main(){ int i, n; // record hold RGB color data RGB_t records [] = {{0,"black", 0x000000}, {1,"blue", 0x0000AA}, {2,"green", 0x00AA00}, {3,"cyan", 0x00AAAA}, {4,"red", 0xAA0000}, {5,"magenta", 0xAA00AA}, {6,"brown", 0xAA5500}, {7,"light gray", 0xAAAAAA}, {8,"dark gray", 0x555555}, {9,"light blue", 0x5555FF}, {10,"light green", 0x55FF55}, {11,"light cyan", 0x55FFFF}, {12,"light red", 0xFF5555}, {13,"light magenta",0xFF55FF}, {14,"yellow", 0xFFFF55}, {15,"white", 0xFFFFFF}};
n = sizeof(records) / sizeof(RGB_t);
printf("List of colors in before sort records are: "); for(i = 0; i < n; i++){ printf(" %d %s %x ", records.id[i], records.color[i], records.value[i]); printf(" "); }
qsort(records, n, sizeof(RGB_t),ColorCompare); printf(" List of colors in afer sort records are: "); for(i = 0; i < n; i++){ printf(" %d %s %x ", records.id[i], records.color[i], records.value[i]); printf(" "); } return 0; } int ColorCompare(const void *pa, const void *pb){ const RGB_t *p1 = pa; const RGB_t *p2 = pb; return strcmp(p1->color, p2->color); }
so I added a bsearch to return the value os color from user input as shown below: for (; ;) { printf(" Enter key like black or red for 24 bit color value:"); /* Get the key, with size limit. */ fgets(input,MAX_KEY_SIZE,stdin); /* Remove trailing newline, if there. and check for end of prog */ if((strlen(input) == 1)) { printf(" ****** end of program ********* "); exit(0); } else { if ((strlen(input) > 0) && (input[strlen (input) - 1] == ' ')){
input[strlen (input) - 1] = '\0'; printf(" KEY entered is %s. ",input); RGB_t key = {.color = input}; RGB_t const *RGB_ptr = bsearch(&key, records, n, sizeof (RGB_t), ColorCompare); if (RGB_ptr != NULL) { printf("Value for Color %s is %5x ", RGB_ptr->color, RGB_ptr->value); } else { printf("
Color %s not found ", input); } } } } I get warnings: [Warning] initialization makes integer from pointer without a cast [Warning] (near initialization for 'key.color[0]') from the line of code: RGB_t key = {.color = input}; when I entered: RGB_t key = {.color = "red"}; i get no error and it returns value from records.
Best video I’ve watched for this topic
Straight up, very clean and easy to understand tutorial! Great explanation!
Not all heroes wear capes.
This is exactly what I was looking for. You made qsort very easy to understand. Thank you.
This video explains exactly what I was looking for with qsort and how it could be implemented for structures, thanks a bunch.
Amazing lesson! So much information that is relevant to what I need to do. I'm very glad you provided a struct example. Bravo my friend.
Superb explanation! Simple but strong. Im trying to make it through CS in College and this is a help!
I absolutely like this video, this is exactly what I was looking for a couple of days.
Well Explained and very nice tutorial. Thank you so much
Past some days i am studing c language...
And believe me no one is like you.
Continue it sir.......
dud do you study in school or without?
Thanks for the beautiful explanation....... Thank u soooo much..
Good content...absolutely loved it 🔥
How are you utilizing *pa and *pb in the compareInt function if you are not passing anything in the arguments from main?
ua-cam.com/video/lG-jfUiT02w/v-deo.html
It's called from inside qsort function. The above link should help you.
Very help full video , nyc explanation ✊🏻thank u sir .
Thank you sir, good explanation
Hi, it would be awesome if you add a section or even a photo of topics you'll cover in a video in your future videos! The video was nice (:
thank you soo much ! great work there .
HIT!! NIEWISKI GO N1ENAWIDZ1 ZNALAZŁ JEDEN PR0STY TRICK NA ZDANIE KOLOSA
HIT !! THE SLAWS HIM HIM FOUND ONE SIMPLE TRICK ON THE COLLECT'S OPINION
(your comment translation)
xD
Thank you so much!! This video is really helpful
very easy way of explanation
Would you please give the link of the PDF manual you followed during explanation the Syntex of qsort?
Its man page.
man qsort
Great explanation sir thank u😀😀
sir, 'pa' and 'pb' are void pointers then how can we pass these two pointers to Strcmp() without typecasting them to char pointer?
In C,
We can assign address of any type to a void pointer without typecasting.
Also the address stored in void pointer can be assigned to typed pointer.
In C++ and in Turbo C, we require typecasting when we assign void pointer to typed pointer.
To make it work, try this
strcmp((char *)pa, (char *)pb);
Very neat. Thank you
very good video,learned a lot
Very nice, thanks, I was thinking I had to write my own sort, and actually, but this is much better.
Professor, I need to solve an array of pointers using qsort. I don't have enough time and I need to submit these projects within 2 weeks in my school. I honestly need help.
thank you!!! really helpful
Well taught!
Thank you! I understand this now!
Professor
I am trying to qsort using name but its not happening.
int cmpname(const void * pa, const void * pb ){
const card_t *p1 = pa;
const card_t *p2 = pb;
// return p1->name - p2->name;
return strcmp(p1->name, p2->name);
}
int n = sizeof(cards) / sizeof(card_t);
qsort(cards, n, sizeof(card_t), cmpname);
What is type of cards, is it multidimensional array or array of pointers?
an array of pointers to structs
I'm getting a segmentation fault when I do this, any suggestion?
Share the code. I will help you in identifying the mistake(s)
whats the value at address "pa" and "pb" ?
Thank u sir😇
Thank you bro...😇😇😇😇
Wooow😊♨️
I want to know reverse shotcut too
Thank you so much ❤️
very helpful video
really helpful
thank you dude
Tried same thing but I kept getting id in something not a structure or union, mycode:
/* QSORT and bsearch that returns a 24-bit color value using
* built in library function
*/
#include
#include
#include
//declare color comparism
int ColorCompare(const void *pa, const void *pb);
//define struct for holding RGB color data
typedef struct _RGB
{
int id;
char color[16];
int value;
}RGB_t;
int main(){
int i, n;
// record hold RGB color data
RGB_t records [] = {{0,"black", 0x000000},
{1,"blue", 0x0000AA},
{2,"green", 0x00AA00},
{3,"cyan", 0x00AAAA},
{4,"red", 0xAA0000},
{5,"magenta", 0xAA00AA},
{6,"brown", 0xAA5500},
{7,"light gray", 0xAAAAAA},
{8,"dark gray", 0x555555},
{9,"light blue", 0x5555FF},
{10,"light green", 0x55FF55},
{11,"light cyan", 0x55FFFF},
{12,"light red", 0xFF5555},
{13,"light magenta",0xFF55FF},
{14,"yellow", 0xFFFF55},
{15,"white", 0xFFFFFF}};
n = sizeof(records) / sizeof(RGB_t);
printf("List of colors in before sort records are:
");
for(i = 0; i < n; i++){
printf(" %d %s %x
", records.id[i], records.color[i], records.value[i]);
printf("
");
}
qsort(records, n, sizeof(RGB_t),ColorCompare);
printf("
List of colors in afer sort records are:
");
for(i = 0; i < n; i++){
printf(" %d %s %x
", records.id[i], records.color[i], records.value[i]);
printf("
");
}
return 0;
}
int ColorCompare(const void *pa, const void *pb){
const RGB_t *p1 = pa;
const RGB_t *p2 = pb;
return strcmp(p1->color, p2->color);
}
Records is an Array
Use records[i].id, records[i].color etc.,
#include
#include
#include
//declare color comparism
int ColorCompare(const void *pa, const void *pb);
//define struct for holding RGB color data
typedef struct _RGB
{
int id;
char color[16];
int value;
}RGB_t;
int main(){
int i, n;
// record hold RGB color data
RGB_t records [] = {{0,"black", 0x000000},
{1,"blue", 0x0000AA},
{2,"green", 0x00AA00},
{3,"cyan", 0x00AAAA},
{4,"red", 0xAA0000},
{5,"magenta", 0xAA00AA},
{6,"brown", 0xAA5500},
{7,"light gray", 0xAAAAAA},
{8,"dark gray", 0x555555},
{9,"light blue", 0x5555FF},
{10,"light green", 0x55FF55},
{11,"light cyan", 0x55FFFF},
{12,"light red", 0xFF5555},
{13,"light magenta",0xFF55FF},
{14,"yellow", 0xFFFF55},
{15,"white", 0xFFFFFF}};
n = sizeof(records) / sizeof(RGB_t);
printf("List of colors in before sort records are:
");
for(i = 0; i < n; i++){
printf(" %d %s %x
", records[i].id, records[i].color, records[i].value);
printf("
");
}
qsort(records, n, sizeof(RGB_t),ColorCompare);
printf("
List of colors in afer sort records are:
");
for(i = 0; i < n; i++){
printf(" %d %s %x
", records[i].id, records[i].color, records[i].value);
printf("
");
}
return 0;
}
int ColorCompare(const void *pa, const void *pb){
const RGB_t *p1 = pa;
const RGB_t *p2 = pb;
return strcmp(p1->color, p2->color);
}
@@programmingwithsikander3539 Thank you for the clarification it worked now .
so I added a bsearch to return the value os color from user input as shown below:
for (; ;) {
printf("
Enter key like black or red for 24 bit color value:");
/* Get the key, with size limit. */
fgets(input,MAX_KEY_SIZE,stdin);
/* Remove trailing newline, if there. and check for end of prog */
if((strlen(input) == 1)) {
printf("
****** end of program *********
");
exit(0);
}
else {
if ((strlen(input) > 0) && (input[strlen (input) - 1] == '
')){
input[strlen (input) - 1] = '\0';
printf("
KEY entered is %s.
",input);
RGB_t key = {.color = input};
RGB_t const *RGB_ptr = bsearch(&key, records, n,
sizeof (RGB_t), ColorCompare);
if (RGB_ptr != NULL) {
printf("Value for Color %s is %5x
", RGB_ptr->color, RGB_ptr->value);
} else {
printf("
Color %s not found
", input);
}
}
}
}
I get warnings:
[Warning] initialization makes integer from pointer without a cast
[Warning] (near initialization for 'key.color[0]')
from the line of code:
RGB_t key = {.color = input};
when I entered:
RGB_t key = {.color = "red"};
i get no error and it returns value from records.
@@skitnado25
Replace
RGB_t key = {.color = input};
with
RGB_t key;
strcpy(key.color , input);
Dude you are my hero! Thank you!
bro how does pa and pb gets the address of the 2 elements in the array? thanku
Watch this video
ua-cam.com/video/lG-jfUiT02w/v-deo.html
Compare function is called within the qsort function.
@@programmingwithsikander3539 thankss a lottt brother ..you made me understood quickly .Very understandable explanation👏👏
thanx very much
Awsome bro
Thank you ❤️
great !!! Thank you :)
Well taught
Thank you!
so clear, ty
please work on your accent
what does his accent have to do with the knowledge imparted? i found it pretty easy to understand