472,791 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

parameter and functionpointer

Hello,

I try to give a parameter in the function tt; but omething went
wrong?? Any idea what i can do??

int ret_integer()
{
return 76;
}
int (*tt(int dummy))()
{

return ((int(*)()) ret_integer());
}
int main()
{ int(*pq)();
pq = tt(23);
pq(); // bang bang
return 0;
}

Thanks a lot C folks
Nov 22 '07 #1
6 1506
humblemark wrote:
Hello,

I try to give a parameter in the function tt; but omething went
wrong?? Any idea what i can do??

As is often the case, when your code starts filling up with casts, it is
probably wrong. The parameter is not the problem: it is the () on the
return value from tt. And the silly cast can't help; it can only mask
errors. Compare the following:

#include <stdio.h>

int ret_integer()
{
return 76;
}

int (*tt(int dummy)) ()
{
(void) dummy;
return ret_integer;
}

int main()
{
int (*pq) ();
pq = tt(23);
printf("pq() = %d\n", pq());
return 0;
}
[OP's code]
int ret_integer()
{
return 76;
}
int (*tt(int dummy))()
{

return ((int(*)()) ret_integer());
}
int main()
{ int(*pq)();
pq = tt(23);
pq(); // bang bang
return 0;
}

Thanks a lot C folks
Nov 22 '07 #2
On 22 Nov, 21:13, humblemark <bloemfonti...@gmail.comwrote:
Hello,

I try to give a parameter in the function tt; but omething went
wrong?? Any idea what i can do??

int ret_integer()
{
return 76;

}

int (*tt(int dummy))()
{

return ((int(*)()) ret_integer());
This line is your problem. ret_integer is a function that returns an
integer. So ret_integer() runs that function, and produces an integer
result (of 76). tt returns that value of 76, horribly cast into a
function pointer. This is, I presume, not what you want. The value you
want out is ret_integer itself, not the result of running it. So miss
off the () after ret_integer, and you get:

return ((int(*)()) ret_integer);

which seems to work fine for me. So well, in fact, that you can miss
out the cast altogether and simply say:

return ret_integer;
}

int main()
{ int(*pq)();
pq = tt(23);
pq(); // bang bang
return 0;

}
Hope that helps.
Paul.
Nov 22 '07 #3
humblemark wrote:
I try to give a parameter in the function tt; but omething went
wrong?? Any idea what i can do??
The first thing you should do is read
<http://www.catb.org/~esr/faqs/smart-questions.html>.

Telling us that "something went wrong" is not particularly useful.
*What* went wrong?

As it happens, you've given us enough information to figure out the problem.
int ret_integer()
{
return 76;
}
Ok, ``ret_integer'' is a function returning an int. It has no
parameters, but it's better to make that explicit; I'd change the
declaration from ``int reg_integer()'' to ``int reg_integer(void)''.
(This isn't your main problem.
int (*tt(int dummy))()
``tt'' is a function that takes an int argument, and returns a pointer
to a function that returns an int. The returned function takes no
arguments; again, it's better to make this explicit:

int (*tt(int dummy))(void)
{

return ((int(*)()) ret_integer());
The expression ``ret_integer()'' *calls* your function and yields the
value that it returns (76, of type int). You then explicitly convert
the int value 76 to a pointer-to-function type, which gives you garbage.

What you want here is not ``ret_integer()'' (the result of calling the
function), but ``ret_integer'' (the name of the function, which decays
to a pointer to the function in this context).

I'm guessing that you added the cast to silence a compile-time error.
That should have made you suspicious. Most (but not all) casts are
actually unnecessary. All you need here is ``return ret_integer;''.
}
int main()
Ok, but ``int main(void)'' is preferred.
{ int(*pq)();
pq = tt(23);
pq(); // bang bang
And here you try to make a function call through the garbage pointer.
The result is undefined behavior (most likely your program crashes).
return 0;
}
Here's a version of your program that displays the result of the call:

#include <stdio.h>

int ret_integer(void)
{
return 76;
}

int (*tt(int dummy))(void)
{
return ret_integer;
}

int main()
{
int(*pq)(void) = tt(23);
printf("pq() returns %d\n", pq());
return 0;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 22 '07 #4
humblemark wrote, On 22/11/07 21:13:
Hello,

I try to give a parameter in the function tt; but omething went
wrong?? Any idea what i can do??
I suggest that you need to learn to use a debugger. Had you done so and
checked the value of pq when you made the call the error would have been
obvious.
int ret_integer()
{
return 76;
}
int (*tt(int dummy))()
Using a typedef for the function type would make this a lot easier to read.
{

return ((int(*)()) ret_integer());
Did you add this case to get rid of a warning? If so you did exactly the
WRONG thing. The case does not fix the code it just stops the compiler
from telling you about your mistake.
return ret_integer;
}
int main()
{ int(*pq)();
pq = tt(23);
pq(); // bang bang
return 0;
}

Thanks a lot C folks
Other general points:

When a function does not take parameters it is better to be explicit so
that the compiler will warn you if you pass parameters in error. E.g.
int (*tt(int dummy))(void)

Using // style comments in posts to Usenet is generally a bad idea
because they do not survive line wrapping.

Please don't use tabs when posting code, sometimes they get stripped by
a server along the way loosing the formatting and other times they are
not rendered by the client in the same way they appear in your editor.
For posting to Usenet always use spaces for indenting.
--
Flash Gordon
Nov 22 '07 #5
On Thu, 22 Nov 2007 13:13:41 -0800 (PST), humblemark
<bl***********@gmail.comwrote:
>Hello,

I try to give a parameter in the function tt; but omething went
wrong?? Any idea what i can do??

int ret_integer()
ret_integer is a function with unspecified arguments returning int
>{
return 76;
}
int (*tt(int dummy))()
tt is a function
with a single (unused) parameter of type int
returning a pointer to function
with unspecified arguments
returning type int

The return type of tt matches ret_integer. So far so good.
>{

return ((int(*)()) ret_integer());
ret_integer() evaluates to an int. You are attempting to cast this
int to a function pointer. I don't think that is what you want. (What
makes you think there is a valid function at address 76?) I think you
want to return the address of ret_integer. You do this by using the
function name without parentheses. This expression evaluates to the
function address with type pointer to function which I think is what
you do want. The statement should read
return ret_integer;

Since this corrected expression is of type pointer to function with
unspecified arguments returning int, the cast serves no purpose since
it tries to convert the expression to the same type. My crystal ball
tells me you added the cast to silence a diagnostic when your original
code said
return ret_integer();
Let this be the example that convinces you that adding a cast to
silence a diagnostic is ALMOST ALWAYS the wrong solution. Only use a
cast when you are absolutely certain it will do what you want.
>}
int main()
{ int(*pq)();
pq = tt(23);
pq(); // bang bang
After you make the above correction, you still have no idea if this
statement worked. Maybe you want to print the value of this
expression.
return 0;
}

Remove del for email
Nov 23 '07 #6
On Nov 23, 1:05 pm, Barry Schwarz <schwa...@doezl.netwrote:
On Thu, 22 Nov 2007 13:13:41 -0800 (PST), humblemark
<bloemfonti...@gmail.comwrote:
Hello,
I try to give a parameter in the function tt; but omething went
wrong?? Any idea what i can do??
int ret_integer()

ret_integer is a function with unspecified arguments returning int
While I wouldn't say it's good practice, an empty parameter list for
the definition of a function means there are no arguments:

6.7.5.3 paragraph 14 (in N1256): "An empty list in a function
declarator that is part of a definition of that function specifies
that the function has no parameters. The empty list in a function
declarator that is not part of a definition of that function specifies
that no information about the number or types of the parameters is
supplied."
Nov 23 '07 #7

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

Similar topics

4
by: Dan | last post by:
I've run into an interesting problem, and seemed to have stumped 3 newsgroups and 2 other forums. For some reason when I try to insert a record into a SQL table that has a Text column, the...
3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
2
by: PK | last post by:
Hi, I have an application that opens a Crystal report document and passes in a value to a parameter in the report (pointing to an Oracle DB). However, if I want to pass a "null" value to retrieve...
7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
4
by: Andreas Klimas | last post by:
hello, no many words, I will start with an example instead int foo(void *r) {...} void test(void) { int *x=0; foo(x); /* seems to me as valid */ }
3
by: Ken Cox [Microsoft MVP] | last post by:
I've been going around and around on this one. I can't believe that it is "by design" as Microsoft says. Here's the situation: In *declarative* syntax, I'm trying to create a default datetime...
5
by: Trevisc | last post by:
Happy Thursday Everyone, I am trying to create a parameter that is one long varchar but that will be used in a SQL statement IN function: //string queryString = GetCurrentTitles(); //Below is...
4
by: A.Gallus | last post by:
in my class I have defined a functionpointer like this: class A { private: Videothing * Video; void (A::*play) (); // <== funcpointer
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.