473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

passing ptr to function to another function

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

Nov 13 '05 #1
4 4987
Pushkar Pradhan <pu*****@gri.msstate.edu> 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
Nov 13 '05 #2
On Sat, 4 Oct 2003 03:30:18 UTC, Pushkar Pradhan
<pu*****@gri.msstate.edu> 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!
Nov 13 '05 #3
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
<pu*****@gri.msstate.edu> 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.


Nov 13 '05 #4
"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote in message
news:3F**************@gri.msstate.edu...
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

Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
12
by: harry | last post by:
I have an object that's passed in to a function as a parameter i.e public boolean getProjectTitle(ProjectHeader_DTO obj) {...} If I then call a method on this object inside the function i.e...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
1
by: User1014 | last post by:
Since you can pass a function to a ... erm...... function.... how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e. ...
5
by: mshaaban | last post by:
Hello, In my code I have a large static 2D arrays defined as: code: #define LONMAX 1440 #define LATMAX60 480 void main (int argc, char *argv)
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.