passing ptr to function to another function

Discussion in 'C Programming' started by Pushkar Pradhan, Oct 4, 2003.

  1. I want a function to execute another function, which I pass to it,
    sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style.

    double exec_basecase(void (*func)(double *a, double *b, double *c),
    double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB)
    {
    ...../*other code*/
    func(a, b, c);
    .....
    }

    This is the call in main():
    double *a = NULL;
    double *b = NULL;
    double *c = NULL;

    /* allocate memory and initialize matrix */
    a = gen_matrix(10, 10, 0);
    b = gen_matrix(10, 10, 0);
    c = gen_matrix(10, 10, 0);

    mflops1[12] = exec_basecase(mm_6r6c_6r6c_bc(a, b, c), a, b, c, 6, 6, 6);

    During compile it gives following warning (in the above call) and then
    during execution crashes inside this function with message: Illegal
    Instruction (core dumped).
    exec_basecases.c: In function `main':
    exec_basecases.c:46: warning: passing arg 1 of `exec_basecase' makes
    pointer from integer without a cast

    Can anyone tell what's wrong with my function call?
    Pushkar Pradhan
    Pushkar Pradhan, Oct 4, 2003
    #1
    1. Advertising

  2. Pushkar Pradhan <> wrote:
    > I want a function to execute another function, which I pass to it,
    > sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style.


    > double exec_basecase(void (*func)(double *a, double *b, double *c),
    > double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB)
    > {
    > ..../*other code*/
    > func(a, b, c);
    > ....
    > }


    > This is the call in main():
    > double *a = NULL;
    > double *b = NULL;
    > double *c = NULL;


    > /* allocate memory and initialize matrix */
    > a = gen_matrix(10, 10, 0);
    > b = gen_matrix(10, 10, 0);
    > c = gen_matrix(10, 10, 0);


    > mflops1[12] = exec_basecase(mm_6r6c_6r6c_bc(a, b, c), a, b, c, 6, 6, 6);


    > During compile it gives following warning (in the above call) and then
    > during execution crashes inside this function with message: Illegal
    > Instruction (core dumped).
    > exec_basecases.c: In function `main':
    > exec_basecases.c:46: warning: passing arg 1 of `exec_basecase' makes
    > pointer from integer without a cast


    > Can anyone tell what's wrong with my function call?
    > Pushkar Pradhan


    The problem lies in the call to exec_basecase

    Here's what you do:

    exec_basecase(mm...(a, b, c), ...);

    Here's what you should do:

    exec_basecase(mm..., ...);

    By placing the parens and arguments there, c assumes that you're
    passing the return value of that function call. The pointer from
    integer warning is because mm_... returns an int, which at least on
    intel architecture, is the representation of a memory address or a
    pointer. c assumes the returned value is the first instruction in
    the function. When exec_basecase tries to execute the function, it
    tries to execute code starting at the location in memory specified
    by the return value of mm_...

    --
    Harrison Caudill | .^ www.hypersphere.org
    Computer Science & Physics Double Major | | Me*Me=1
    Georgia Institute of Technology | '/ I'm just a normal guy
    Charles Harrison Caudill, Oct 4, 2003
    #2
    1. Advertising

  3. On Sat, 4 Oct 2003 03:30:18 UTC, Pushkar Pradhan
    <> wrote:

    > I want a function to execute another function, which I pass to it,
    > sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style.
    >
    > double exec_basecase(void (*func)(double *a, double *b, double *c),
    > double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB)
    > {
    > ..../*other code*/
    > func(a, b, c);
    > ....
    > }
    >
    > This is the call in main():
    > double *a = NULL;
    > double *b = NULL;
    > double *c = NULL;
    >
    > /* allocate memory and initialize matrix */
    > a = gen_matrix(10, 10, 0);
    > b = gen_matrix(10, 10, 0);
    > c = gen_matrix(10, 10, 0);
    >
    > mflops1[12] = exec_basecase(mm_6r6c_6r6c_bc(a, b, c), a, b, c, 6, 6, 6);


    This makes the steps:
    calling mm_6r6c_6r6c_ba with the parameters a, b and c, then calling
    exec_basecases with the parameters: result from the prior step, a, b,
    c, 6,6,6

    That is not that you have described above and the protoype says.

    change it to
    exec_basecases(mm_6r6c_6rc6c_bca, a,b,c, 6, 6, 6);
    This delivers to exec_basecases the address of a function that gets 3
    parameters. Which parameter that function gets is defined on the point
    exec_basename calls the funtion its address it gets here.

    > During compile it gives following warning (in the above call) and then
    > during execution crashes inside this function with message: Illegal
    > Instruction (core dumped).
    > exec_basecases.c: In function `main':
    > exec_basecases.c:46: warning: passing arg 1 of `exec_basecase' makes
    > pointer from integer without a cast
    >
    > Can anyone tell what's wrong with my function call?


    Clearly you delivers the result of the fuction instead its address to
    exec_basecases.

    A call of a function is function_name(parameter list).
    The address of a fuction is its naked name. Remove the parameterlist
    and the brackets from the function name you want that ecec_basecases
    should call itself.


    --
    Tschau/Bye
    Herbert

    eComStation 1.1 Deutsch wird jetzt ausgeliefert!
    The Real OS/2 Guy, Oct 4, 2003
    #3
  4. Both the responses to my post were helpful to solve and make me
    understand, however I have this example from Kernighan and Richie:

    void qsort(void *lineptr[], int left, int right, int (*comp)(void*, void
    *));

    int main()
    {
    /*other code*/
    if((nlines = readlines(lineptr, MAXLINES)) >= 0) {
    qsort((void **) lineptr, 0, nlines-1,
    int (*)(void*, void*))(numeric ? numcmp : strcmp));

    comp() is a comparison function and numeric variable decides which
    function to use.

    How come they pass the return type in the function call? Just a curiousity.
    The Real OS/2 Guy wrote:
    > On Sat, 4 Oct 2003 03:30:18 UTC, Pushkar Pradhan
    > <> wrote:
    >
    >
    >>I want a function to execute another function, which I pass to it,
    >>sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style.
    >>
    >>double exec_basecase(void (*func)(double *a, double *b, double *c),
    >>double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB)
    >>{
    >>..../*other code*/
    >> func(a, b, c);
    >>....
    >>}
    >>
    >>This is the call in main():
    >> double *a = NULL;
    >> double *b = NULL;
    >> double *c = NULL;
    >>
    >> /* allocate memory and initialize matrix */
    >> a = gen_matrix(10, 10, 0);
    >> b = gen_matrix(10, 10, 0);
    >> c = gen_matrix(10, 10, 0);
    >>
    >> mflops1[12] = exec_basecase(mm_6r6c_6r6c_bc(a, b, c), a, b, c, 6, 6, 6);

    >
    >
    > This makes the steps:
    > calling mm_6r6c_6r6c_ba with the parameters a, b and c, then calling
    > exec_basecases with the parameters: result from the prior step, a, b,
    > c, 6,6,6
    >
    > That is not that you have described above and the protoype says.
    >
    > change it to
    > exec_basecases(mm_6r6c_6rc6c_bca, a,b,c, 6, 6, 6);
    > This delivers to exec_basecases the address of a function that gets 3
    > parameters. Which parameter that function gets is defined on the point
    > exec_basename calls the funtion its address it gets here.
    >
    >
    >>During compile it gives following warning (in the above call) and then
    >>during execution crashes inside this function with message: Illegal
    >>Instruction (core dumped).
    >>exec_basecases.c: In function `main':
    >>exec_basecases.c:46: warning: passing arg 1 of `exec_basecase' makes
    >>pointer from integer without a cast
    >>
    >>Can anyone tell what's wrong with my function call?

    >
    >
    > Clearly you delivers the result of the fuction instead its address to
    > exec_basecases.
    >
    > A call of a function is function_name(parameter list).
    > The address of a fuction is its naked name. Remove the parameterlist
    > and the brackets from the function name you want that ecec_basecases
    > should call itself.
    >
    >
    Pushkar Pradhan, Oct 4, 2003
    #4
  5. Pushkar Pradhan

    Mike Wahler Guest

    "Pushkar Pradhan" <> wrote in message
    news:...
    > Both the responses to my post were helpful to solve and make me
    > understand, however I have this example from Kernighan and Richie:
    >
    > void qsort(void *lineptr[], int left, int right, int (*comp)(void*, void


    Note that this is not the prototype for the standard library
    function 'qsort'. I see that there are a couple example
    implementations of a 'qsort' funtion in the book, but
    they're not the standard library function, where the
    meanings of the second and third arguments are not
    the same.

    For the standard library qsort, the second and
    third parameters have type 'size_t', not 'int'.
    The second parameter is the number elements of
    the array to be sorted, the third is the size of
    each array element.

    > *));
    >
    > int main()
    > {
    > /*other code*/
    > if((nlines = readlines(lineptr, MAXLINES)) >= 0) {
    > qsort((void **) lineptr, 0, nlines-1,
    > int (*)(void*, void*))(numeric ? numcmp : strcmp));
    >
    > comp() is a comparison function


    'comp' is the name of a parameter which has a pointer type.
    It's not a function.

    >and numeric variable decides which
    > function to use.


    Yes, 'numeric' is being used as a selector for
    which function to invoke.

    >
    > How come they pass the return type in the function call?


    Types are not passed as arguments, values are.
    The address of one of the two functions 'numcmp'
    and 'strcmp' is being passed, dependent upon the
    'truth value' of 'numeric'.

    -Mike
    Mike Wahler, Oct 4, 2003
    #5
    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. Sid
    Replies:
    5
    Views:
    1,063
  2. Replies:
    0
    Views:
    330
  3. Replies:
    22
    Views:
    737
    peter koch
    Apr 30, 2008
  4. Replies:
    6
    Views:
    332
    James Kanze
    Apr 29, 2008
  5. DSF
    Replies:
    7
    Views:
    83
    James Kuyper
    Jun 12, 2014
Loading...

Share This Page