callback function

B

Bill Cunningham

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);
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
 
J

John Gordon

In said:
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.
 
B

Barry Schwarz

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);


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

Bill Cunningham

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



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

Ian Collins

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?
 
J

James Kuyper

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



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).
 
B

Bill Cunningham

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


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.


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

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);


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
 
J

John Gordon

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!)
 
B

Ben Bacarisse

John Gordon said:
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.
 
B

Bill Cunningham

Bill said:
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);
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
 
I

Ian Collins

Bill said:
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;
 
B

Bill Cunningham

Ian said:
Bill said:
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.
 
K

Keith Thompson

Bill Cunningham said:
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.
 
I

Ian Collins

Ian said:
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.
 
P

Phil Carmody

Keith Thompson said:
Bill Cunningham said:
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
 
B

Bill Cunningham

Keith said:
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
 
B

Bill Cunningham

Ian said:
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
 
B

Bill Cunningham

Ian said:
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
 
B

Bill Cunningham

Ian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top