Click here to Skip to main content
15,921,840 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questioncode analysis Pin
Member 1507028614-Feb-21 20:29
Member 1507028614-Feb-21 20:29 
AnswerRe: code analysis Pin
Richard MacCutchan14-Feb-21 22:16
mveRichard MacCutchan14-Feb-21 22:16 
GeneralRe: code analysis Pin
Member 1507028615-Feb-21 22:57
Member 1507028615-Feb-21 22:57 
AnswerRe: code analysis Pin
Greg Utas15-Feb-21 0:52
professionalGreg Utas15-Feb-21 0:52 
GeneralRe: code analysis Pin
Member 1507028615-Feb-21 23:02
Member 1507028615-Feb-21 23:02 
Questioncallbacks in c Pin
Member 1506971810-Feb-21 5:16
Member 1506971810-Feb-21 5:16 
AnswerRe: callbacks in c Pin
Greg Utas10-Feb-21 5:34
professionalGreg Utas10-Feb-21 5:34 
AnswerRe: callbacks in c Pin
k505410-Feb-21 6:35
mvek505410-Feb-21 6:35 
You might already be familiar with callbacks and not realize it. If you've ever used qsort() or bsearch(), or another library function that takes a function pointer as an argument to help it do its work, you have already used callbacks!

For example the signature for qsort is
C
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
and might be used like this
C
struct foo {
 char key[10];
 int id;
 /* more members */
};

/* comparison function to sort struct foo by key */
int cmpfoo_bykey(const void *vp1, const void *vp2)
{
  const struct foo *foo1 = vp1;
  const struct foo *foo2 = vp2;

  return strcmp(foo1->key, foo2->key);
}

/* comparison function to sort struct foo by id */
int cmpfoo_byid(const void *vp1, const void *vp2)
{
  const struct foo *foo1 = vp1;
  const struct foo *foo2 = vp2;

  return foo1->id - foo2->id;
}

int main()
{
   struct foo my_data[100];
   /* load data into my_data : not show here*/

   /* sort my_data by key field */
   qsort(my_data, 100, sizeof(struct foo), cmpfoo_bykey);

   /* use my_data in key-ordered way ... */

   /* sort my_data by id field */
   qsort(my_data, 100, sizeof(struct foo), cmpfoo_byid);

  /* use my_data in id-ordered way ... */

  return 0;
}

In the above example qsort() knows how to sort an array of items in a generic way, but not how to compare items. It gets around this limitation by using a callback function, that the programmer provides, that does know how to compare items in the array. So, whenever qsort() needs to know how two items in the array compare to each other, it calls back to the code provided by the programmer to determine how the two items compare to each other.
Keep Calm and Carry On

QuestionFunction similar to printf for software UART, without the need for a large temporary buffer? Pin
arnold_w8-Feb-21 11:20
arnold_w8-Feb-21 11:20 
AnswerRe: Function similar to printf for software UART, without the need for a large temporary buffer? Pin
Mircea Neacsu8-Feb-21 12:00
Mircea Neacsu8-Feb-21 12:00 
GeneralRe: Function similar to printf for software UART, without the need for a large temporary buffer? Pin
arnold_w9-Feb-21 7:39
arnold_w9-Feb-21 7:39 
GeneralRe: Function similar to printf for software UART, without the need for a large temporary buffer? Pin
Mircea Neacsu9-Feb-21 8:22
Mircea Neacsu9-Feb-21 8:22 
AnswerRe: Function similar to printf for software UART, without the need for a large temporary buffer? Pin
CPallini8-Feb-21 20:11
mveCPallini8-Feb-21 20:11 
GeneralRe: Function similar to printf for software UART, without the need for a large temporary buffer? Pin
arnold_w9-Feb-21 8:04
arnold_w9-Feb-21 8:04 
GeneralRe: Function similar to printf for software UART, without the need for a large temporary buffer? Pin
CPallini9-Feb-21 19:57
mveCPallini9-Feb-21 19:57 
QuestionChanging gamma with multiple display setup Pin
Valentinor6-Feb-21 1:23
Valentinor6-Feb-21 1:23 
AnswerRe: Changing gamma with multiple display setup Pin
Richard MacCutchan6-Feb-21 1:30
mveRichard MacCutchan6-Feb-21 1:30 
GeneralRe: Changing gamma with multiple display setup Pin
Valentinor6-Feb-21 1:55
Valentinor6-Feb-21 1:55 
GeneralRe: Changing gamma with multiple display setup Pin
Richard MacCutchan6-Feb-21 2:10
mveRichard MacCutchan6-Feb-21 2:10 
GeneralRe: Changing gamma with multiple display setup Pin
Valentinor6-Feb-21 2:56
Valentinor6-Feb-21 2:56 
GeneralRe: Changing gamma with multiple display setup Pin
Dave Kreskowiak6-Feb-21 5:05
mveDave Kreskowiak6-Feb-21 5:05 
GeneralRe: Changing gamma with multiple display setup Pin
Valentinor6-Feb-21 21:34
Valentinor6-Feb-21 21:34 
GeneralRe: Changing gamma with multiple display setup Pin
Dave Kreskowiak7-Feb-21 4:30
mveDave Kreskowiak7-Feb-21 4:30 
GeneralRe: Changing gamma with multiple display setup Pin
Valentinor7-Feb-21 4:46
Valentinor7-Feb-21 4:46 
GeneralRe: Changing gamma with multiple display setup Pin
Dave Kreskowiak7-Feb-21 6:29
mveDave Kreskowiak7-Feb-21 6:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.