Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   callback function (http://www.velocityreviews.com/forums/t953518-callback-function.html)

Bill Cunningham 10-17-2012 09:16 PM

callback function
 
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



John Gordon 10-17-2012 09:36 PM

Re: callback function
 
In <k5n77p$e5v$1@speranza.aioe.org> "Bill Cunningham" <billcun@suddenlink.net> 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
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


Barry Schwarz 10-17-2012 09:39 PM

Re: callback function
 
On Wed, 17 Oct 2012 17:16:51 -0400, "Bill Cunningham"
<billcun@suddenlink.net> 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

Bill Cunningham 10-17-2012 09:44 PM

Re: callback function
 
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.



Ian Collins 10-17-2012 09:45 PM

Re: callback function
 
On 10/18/12 10:36, John Gordon wrote:
> In<k5n77p$e5v$1@speranza.aioe.org> "Bill Cunningham"<billcun@suddenlink.net> 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

James Kuyper 10-17-2012 09:47 PM

Re: callback function
 
On 10/17/2012 05:36 PM, John Gordon wrote:
> In <k5n77p$e5v$1@speranza.aioe.org> "Bill Cunningham" <billcun@suddenlink.net> 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).


Bill Cunningham 10-17-2012 09:50 PM

Re: callback function
 
Barry Schwarz wrote:
> On Wed, 17 Oct 2012 17:16:51 -0400, "Bill Cunningham"
> <billcun@suddenlink.net> 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



John Gordon 10-17-2012 09:51 PM

Re: callback function
 
In <k5n8sj$hph$1@speranza.aioe.org> "Bill Cunningham" <billcun@suddenlink.net> 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
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


Ian Collins 10-17-2012 10:00 PM

Re: callback function
 
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

Ben Bacarisse 10-17-2012 11:11 PM

Re: callback function
 
John Gordon <gordon@panix.com> writes:

> In <k5n8sj$hph$1@speranza.aioe.org> "Bill Cunningham" <billcun@suddenlink.net> 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.


All times are GMT. The time now is 06:29 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.