callback function

Discussion in 'C Programming' started by Bill Cunningham, Oct 17, 2012.

  1. 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
     
    Bill Cunningham, Oct 17, 2012
    #1
    1. Advertising

  2. Bill Cunningham

    John Gordon Guest

    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"
     
    John Gordon, Oct 17, 2012
    #2
    1. Advertising

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


    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
     
    Barry Schwarz, Oct 17, 2012
    #3
  4. 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.
     
    Bill Cunningham, Oct 17, 2012
    #4
  5. Bill Cunningham

    Ian Collins Guest

    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
     
    Ian Collins, Oct 17, 2012
    #5
  6. Bill Cunningham

    James Kuyper Guest

    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).
     
    James Kuyper, Oct 17, 2012
    #6
  7. 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);

    >
    > 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
     
    Bill Cunningham, Oct 17, 2012
    #7
  8. Bill Cunningham

    John Gordon Guest

    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"
     
    John Gordon, Oct 17, 2012
    #8
  9. Bill Cunningham

    Ian Collins Guest

    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
     
    Ian Collins, Oct 17, 2012
    #9
  10. 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.
     
    Ben Bacarisse, Oct 18, 2012
    #10
  11. 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);
    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
     
    Bill Cunningham, Oct 18, 2012
    #11
  12. Bill Cunningham

    Ian Collins Guest

    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
     
    Ian Collins, Oct 18, 2012
    #12
  13. 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.
     
    Bill Cunningham, Oct 18, 2012
    #13
  14. "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) <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"
     
    Keith Thompson, Oct 18, 2012
    #14
  15. Bill Cunningham

    Ian Collins Guest

    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
     
    Ian Collins, Oct 18, 2012
    #15
  16. Bill Cunningham

    Phil Carmody Guest

    Keith Thompson <> 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 /.
     
    Phil Carmody, Oct 18, 2012
    #16
  17. 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
     
    Bill Cunningham, Oct 18, 2012
    #17
  18. 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
     
    Bill Cunningham, Oct 18, 2012
    #18
  19. 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
     
    Bill Cunningham, Oct 18, 2012
    #19
  20. 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
     
    Bill Cunningham, Oct 18, 2012
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prettysmurfed
    Replies:
    6
    Views:
    705
    prettysmurfed
    Jul 22, 2003
  2. pvdm
    Replies:
    1
    Views:
    698
    tom_usenet
    Sep 9, 2003
  3. JDT
    Replies:
    6
    Views:
    552
    James Kanze
    Mar 29, 2007
  4. Angus
    Replies:
    32
    Views:
    827
    Richard
    Apr 15, 2008
  5. Ramesh
    Replies:
    11
    Views:
    4,260
    James Kanze
    Dec 27, 2008
Loading...

Share This Page