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
I have these 3 functions and before I compile I am going to ask about
them. This is for qsort. I tried to tackle this once and couldn't get it I
think I get it now. I don't like const's. They are a nuisance. You can't
directly pass string literals to them you want to change. Maybe that's just
the way C is though.

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

int comp(constvoid *a,const void *b)
{
int *ia=(constint *)a;
int *ib=(const int *)b;
return *ia-*ib;
}

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

void sort_int(void)
{
int num={1,3,6,4,5};
size_t num_len=sizeof num/sizeof (int);
}

1 because of qsorts comp callback function parameters include a const do I
need them and can I remove them as I have in the callback?
2. second line of sort_int. num is not a built in type so it doesn't require
parenthesis does it?

Bill


 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      10-17-2012
In <k5n77p$e5v$> "Bill Cunningham" <> writes:

> I don't like const's. They are a nuisance. You can't directly pass string
> literals to them you want to change.


Why are you passing something as const when you want to change it? That's
kind of the opposite of const.

> void sort_int(void)
> {
> int num={1,3,6,4,5};
> size_t num_len=sizeof num/sizeof (int);
> }


> 2. second line of sort_int. num is not a built in type so it doesn't require
> parenthesis does it?


Sizeof is a keyword, not a function call. It never needs parentheses.

Also, you forgot to declare num as an array.

--
John Gordon A is for Amy, who fell down the stairs
B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      10-17-2012
On Wed, 17 Oct 2012 17:16:51 -0400, "Bill Cunningham"
<> wrote:

> I have these 3 functions and before I compile I am going to ask about
>them. This is for qsort. I tried to tackle this once and couldn't get it I
>think I get it now. I don't like const's. They are a nuisance. You can't
>directly pass string literals to them you want to change. Maybe that's just
>the way C is though.


Since changing a string literal is prohibited, you are doomed before
you start.

>
>#include <stdio.h>
>#include <stdlib.h>
>
>int comp(constvoid *a,const void *b)


Wouldn't using cut and paste be easier than retyping? It would also
eliminate silly errors like parameter 1 as well as provide some clue
that this is your actual code.

>{
> int *ia=(constint *)a;


Doesn't this generate a diagnostic? Either you make ia const or you
cast away the const from a.

> int *ib=(const int *)b;
> return *ia-*ib;


Are you sure this never overflows?

>}
>
>void print_int_array(int *array,size_t len)
>{
> size_t i;
> for (i=0;i<len;i++)


Did you run out of horizontal white space or do you just enjoy
cramming your text together to make it harder to read?

> printf("%d | ",array[i]);


Still having problems with indenting?

> putchar('\n');
>}
>
>void sort_int(void)
>{
> int num={1,3,6,4,5};


Did you mean for num to be an array?

> size_t num_len=sizeof num/sizeof (int);
>}
>
>1 because of qsorts comp callback function parameters include a const do I
>need them and can I remove them as I have in the callback?


You didn't remove any const in comp(). Maybe you meant to but are
just posting code of the top of your head.

>2. second line of sort_int. num is not a built in type so it doesn't require
>parenthesis does it?


What???? num is an object of type int which certainly is built-in. Of
course, you meant for it to be an array of int which is a built-in
aggregate. But then it would require brackets.


--
Remove del for email
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      10-17-2012
John Gordon wrote:

> Why are you passing something as const when you want to change it?
> That's
> kind of the opposite of const.
>
>> void sort_int(void)
>> {
>> int num={1,3,6,4,5};
>> size_t num_len=sizeof num/sizeof (int);
>> }

>
>> 2. second line of sort_int. num is not a built in type so it doesn't
>> require parenthesis does it?

>
> Sizeof is a keyword, not a function call. It never needs parentheses.


try sizeof int and see if that works. If not trry sizeof (int). It
doesn't work on my machine.

> Also, you forgot to declare num as an array.


oops. I see that.


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-17-2012
On 10/18/12 10:36, John Gordon wrote:
> In<k5n77p$e5v$> "Bill Cunningham"<> writes:
>
>> void sort_int(void)
>> {
>> int num={1,3,6,4,5};
>> size_t num_len=sizeof num/sizeof (int);
>> }

>
>> 2. second line of sort_int. num is not a built in type so it doesn't require
>> parenthesis does it?

>
> Sizeof is a keyword, not a function call. It never needs parentheses.


Except, as in this case, where its argument is a type.

See what happens when you get sucked into a Bill troll?

--
Ian Collins
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-17-2012
On 10/17/2012 05:36 PM, John Gordon wrote:
> In <k5n77p$e5v$> "Bill Cunningham" <> writes:
>
>> I don't like const's. They are a nuisance. You can't directly pass string
>> literals to them you want to change.

>
> Why are you passing something as const when you want to change it? That's
> kind of the opposite of const.
>
>> void sort_int(void)
>> {
>> int num={1,3,6,4,5};
>> size_t num_len=sizeof num/sizeof (int);
>> }

>
>> 2. second line of sort_int. num is not a built in type so it doesn't require
>> parenthesis does it?

>
> Sizeof is a keyword, not a function call. It never needs parentheses.


The language is case sensitive - that's sizeof, not Sizeof.

The grammar productions involving sizeof are:
sizeof unary-expression
sizeof ( type-name )

Thus, parentheses are mandatory when the operand is a type name (as in
sizeof(int) ) rather than a unary expression (as in sizeof num).

 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      10-17-2012
Barry Schwarz wrote:
> On Wed, 17 Oct 2012 17:16:51 -0400, "Bill Cunningham"
> <> wrote:
>
>> I have these 3 functions and before I compile I am going to ask
>> about them. This is for qsort. I tried to tackle this once and
>> couldn't get it I think I get it now. I don't like const's. They are
>> a nuisance. You can't directly pass string literals to them you want
>> to change. Maybe that's just the way C is though.

>
> Since changing a string literal is prohibited, you are doomed before
> you start.
>
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int comp(constvoid *a,const void *b)

>
> Wouldn't using cut and paste be easier than retyping? It would also
> eliminate silly errors like parameter 1 as well as provide some clue
> that this is your actual code.
>
>> {
>> int *ia=(constint *)a;

>
> Doesn't this generate a diagnostic? Either you make ia const or you
> cast away the const from a.
>
>> int *ib=(const int *)b;
>> return *ia-*ib;


Ok like int *ia=(int *)b; ?

> Are you sure this never overflows?


Like I said at the beginning of my post. This is untested code.

>> }
>>
>> void print_int_array(int *array,size_t len)
>> {
>> size_t i;
>> for (i=0;i<len;i++)

>
> Did you run out of horizontal white space or do you just enjoy
> cramming your text together to make it harder to read?
>
>> printf("%d | ",array[i]);

>
> Still having problems with indenting?
>
>> putchar('\n');
>> }
>>
>> void sort_int(void)
>> {
>> int num={1,3,6,4,5};

>
> Did you mean for num to be an array?
>
>> size_t num_len=sizeof num/sizeof (int);
>> }
>>
>> 1 because of qsorts comp callback function parameters include a
>> const do I need them and can I remove them as I have in the callback?

>
> You didn't remove any const in comp(). Maybe you meant to but are
> just posting code of the top of your head.
>
>> 2. second line of sort_int. num is not a built in type so it doesn't
>> require parenthesis does it?

>
> What???? num is an object of type int which certainly is built-in. Of
> course, you meant for it to be an array of int which is a built-in
> aggregate. But then it would require brackets.


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

Bill


 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      10-17-2012
In <k5n8sj$hph$> "Bill Cunningham" <> writes:

> > Sizeof is a keyword, not a function call. It never needs parentheses.


> try sizeof int and see if that works. If not trry sizeof (int). It
> doesn't work on my machine.


Whoops, I was mistaken. If you're getting the size of a variable you
don't need parentheses. But if you're getting the size of a type, you do
need them.

(And it's an operator, not a keyword. Wrong all around!)

--
John Gordon A is for Amy, who fell down the stairs
B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-17-2012
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.

--
Ian Collins
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-17-2012
John Gordon <> writes:

> In <k5n8sj$hph$> "Bill Cunningham" <> writes:
>
>> > Sizeof is a keyword, not a function call. It never needs parentheses.

>
>> try sizeof int and see if that works. If not trry sizeof (int). It
>> doesn't work on my machine.

>
> Whoops, I was mistaken. If you're getting the size of a variable you
> don't need parentheses. But if you're getting the size of a type, you do
> need them.
>
> (And it's an operator, not a keyword. Wrong all around!)


No, it's not as bad as that. It *is* a keyword. It's an operator, too,
but that does not stop it being a keyword.

--
Ben.
 
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