Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > callback function

Reply
Thread Tools

callback function

 
 
Bill Cunningham
Guest
Posts: n/a
 
      10-17-2012
Bill Cunningham wrote:

> Yes I forgot the []. Ok I will rewrite.


Now this seems to work.

#include <stdio.h>
#include <stdlib.h>

int comp(const void *a, const void *b)
{
int *pa = (int *) a;
int *pb = (int *) b;
return *pa - *pb;
}

void print_int_array(int *array, size_t len)
{
size_t i = 0;
for (; i < len; i++)
printf("%d | ", array[i]);
putchar('\n');
}

int main(void)
{
int num[] = { 3, 4, 1, 2, 5 };
size_t num_len = sizeof(num) / sizeof(int);
qsort(num, num_len, sizeof(int), comp);
print_int_array(num, num_len);
}

And it's perfectly indented just for you Barry.

Bill


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-18-2012
On 10/18/12 12:25, Bill Cunningham wrote:
> Bill Cunningham wrote:
>
>> Yes I forgot the []. Ok I will rewrite.

>
> Now this seems to work.
>
> #include<stdio.h>
> #include<stdlib.h>
>
> int comp(const void *a, const void *b)
> {
> int *pa = (int *) a;
> int *pb = (int *) b;
> return *pa - *pb;
> }


Why do you keep those silly casts? Just write

const int *pa = a;
const int *pb = b;

--
Ian Collins
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      10-18-2012
Ian Collins wrote:
> On 10/18/12 12:25, Bill Cunningham wrote:
>> Bill Cunningham wrote:
>>
>>> Yes I forgot the []. Ok I will rewrite.

>>
>> Now this seems to work.
>>
>> #include<stdio.h>
>> #include<stdlib.h>
>>
>> int comp(const void *a, const void *b)
>> {
>> int *pa = (int *) a;
>> int *pb = (int *) b;
>> return *pa - *pb;
>> }

>
> Why do you keep those silly casts? Just write
>
> const int *pa = a;
> const int *pb = b;


I'm more interested in why the dereferences. If I'm dereferencing pa and pb
then I must be using or change the object they're pointing to which must be
the generic void *a and void *b.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-18-2012
"Bill Cunningham" <> writes:
> I have these 3 functions and before I compile I am going to ask about
> them.

[...]

You're posting code that you haven't even tried to compile?

Please don't waste our time.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-18-2012
On 10/18/12 13:41, Bill Cunningham wrote:
> Ian Collins wrote:
>> On 10/18/12 12:25, Bill Cunningham wrote:
>>> Bill Cunningham wrote:
>>>
>>>> Yes I forgot the []. Ok I will rewrite.
>>>
>>> Now this seems to work.
>>>
>>> #include<stdio.h>
>>> #include<stdlib.h>
>>>
>>> int comp(const void *a, const void *b)
>>> {
>>> int *pa = (int *) a;
>>> int *pb = (int *) b;
>>> return *pa - *pb;
>>> }

>>
>> Why do you keep those silly casts? Just write
>>
>> const int *pa = a;
>> const int *pb = b;

>
> I'm more interested in why the dereferences. If I'm dereferencing pa and pb
> then I must be using or change the object they're pointing to which must be
> the generic void *a and void *b.


The whole point of the parameters being const qualified is you can't
change values pointed to.

--
Ian Collins
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      10-18-2012
Keith Thompson <kst-> writes:
> "Bill Cunningham" <> writes:
> > I have these 3 functions and before I compile I am going to ask about
> > them.

> [...]
>
> You're posting code that you haven't even tried to compile?
>
> Please don't waste our time.


Why didn't Bill hit everyone's killfiles half a decade ago?
Your advice is heading straight down /dev/null every time you
address his wibblings.

Phil
--
Regarding TSA regulations:
How are four small bottles of liquid different from one large bottle?
Because four bottles can hold the components of a binary liquid explosive,
whereas one big bottle can't. -- camperdave responding to MacAndrew on /.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      10-18-2012
Keith Thompson wrote:

> You're posting code that you haven't even tried to compile?
>
> Please don't waste our time.


Why do some people post untested code? I didn't know it was against the
rules. I've seen it alot. It's tested now and works perfectly.

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      10-18-2012
Ian Collins wrote:

> The whole point of the parameters being const qualified is you can't
> change values pointed to.


Yes I understand. I am just expressing the opinion like those in the
*in* group that you can just not change it. I think it's a waste to the
standard personally. FWIW.

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      10-18-2012
Ian Collins wrote:
> On 10/18/12 10:50, Bill Cunningham wrote:
>>
>> Like I said at the beginning of my post. This is untested code.

>
> Why post untested code? Test it, than ask for help.


I've often wondered why post untested code too that has been posted by
those in the *in* group. They do it I didn't know it was against the rules.
Atleast for some.

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      10-18-2012
Ian Collins wrote:
> On 10/18/12 10:50, Bill Cunningham wrote:
>>
>> Like I said at the beginning of my post. This is untested code.

>
> Why post untested code? Test it, than ask for help.


Besides I thought untested code was enough in answering my questions. I
planned on testing it and have.

And let me ask you as a programmer Ian and being in the *in* group an
*outgroup*er question. What exactly do callback pointer functions help with.
I know this should come from the womb but just and honest question.

Bill


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning a member function to signal callback function pointer Ramesh C++ 11 12-27-2008 09:36 AM
How do I create a function in my library for passing user callback function Angus C Programming 32 04-15-2008 02:28 PM
can a class member function be used as a callback function? JDT C++ 6 03-29-2007 12:45 PM
Using function pointer in callback function pvdm C++ 1 09-09-2003 12:26 PM
Howto: Class member function as callback function for dialog box prettysmurfed C++ 6 07-22-2003 06:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57