Hello,
I ran across the following code, and it blew me away (I've been
programming in C for 3 months now). I've checked tutorials, books,
etc., but I can't find any idioms such as below. Can someone help me
decipher it ?
#include <stdio.h>
typedef int (* f_p)(int); /* 1: what does this do? */
static int k = 0;
int add (int x) { return k + x; } ; /* 2: what is this? ending with ;
? */
f_p addn(int n)
{
k = n;
return add ; /* 3: what is all this? Return value? */
}
int main ( int argv, char** argc ) {
printf("Calling (*addn(12))(13): %d\n",
(addn(12))(13));
printf("Calling (*addn(1))(13): %d\n",
(addn(1))(13));
printf("Calling (*addn(2))(3): %d\n",
(addn(2))(3));
return 0;
}
===
I have a feeling addn returns a pointer that was defined in typedef.
I've never seen such a thing before, but it seems like such a return
value is hard to show in the actual function header so the typedef is
used. As for the rest, I am totally lost.
Thx 2 1430
G Patel wrote: Hello,
I ran across the following code, and it blew me away (I've been programming in C for 3 months now). I've checked tutorials, books, etc., but I can't find any idioms such as below. Can someone help me decipher it ?
#include <stdio.h>
typedef int (* f_p)(int); /* 1: what does this do? */
It says that `f_p' is a synonym for the type: "a pointer to a function
that takes an `int' as an argument and returns an `int'.
static int k = 0;
int add (int x) { return k + x; } ; /* 2: what is this? ending with ; ? */
It's a stray `;' (an empty statement). f_p addn(int n) { k = n; return add ; /* 3: what is all this? Return value? */
Yes. It is returning a pointer to the function `add', after setting the
static int `k' to the desired value. }
The return type is `f_p', as explained above. int main ( int argv, char** argc ) {
printf("Calling (*addn(12))(13): %d\n", (addn(12))(13)); printf("Calling (*addn(1))(13): %d\n", (addn(1))(13)); printf("Calling (*addn(2))(3): %d\n", (addn(2))(3));
return 0; }
===
I have a feeling addn returns a pointer that was defined in typedef. I've never seen such a thing before, but it seems like such a return value is hard to show in the actual function header so the typedef is used. As for the rest, I am totally lost.
It's just a fragile fake closure.
HTH,
--ag
--
Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays
On Sun, 23 Jan 2005 16:45:06 -0600, Artie Gold wrote: G Patel wrote:
.... static int k = 0;
int add (int x) { return k + x; } ; /* 2: what is this? ending with ; ? */ It's a stray `;' (an empty statement).
Statements can only occur within function bodies, this is a syntax error.
Lawrence This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Antony Sequeira |
last post by:
Hi
I needed to share some configuration information across requests.
The most straightforward way would be code it in a php file and include it
in every other script.
The problem with this...
|
by: pekka niiranen |
last post by:
Hi there,
I have perl script that uses dynamically
constructed regular in this way:
------perl code starts ----
$result "";
$key = AAA\?01;
$key = quotemeta $key;
$line = " ...
|
by: Steven T. Hatton |
last post by:
This is something I've been looking at because it is central to a currently
broken part of the KDevelop new application wizard. I'm not complaining
about it being broken, It's a CVS images. ...
|
by: Fan Ruo Xin |
last post by:
Hi,
We only purchased db2 udb license for AIX platforms. So I have to install
the DB2 UDB Version8.1 tried code on my laptop (Win2k). I haven't used it
for a long time. When I tried to bring the...
|
by: Cowboy \(Gregory A. Beamer\) |
last post by:
Background:
-------------
The idea started as a single sign on type of application. Having tested it
before, I knew we could institute single sign on using the same
Authentication Cookie name (in...
|
by: Mark |
last post by:
Ok, I know that .net inherently does not share session data across asp.net
projects, but is there any decent work around to this.
We already have a big chunk of our application using the asp.net...
|
by: MisterKen |
last post by:
It appears that I'm losing values for session variables when I move from a
page like http://www.my_site.com/catalog.aspx to
https://www50.ssldomain.com/my_site/login.aspx and vice versa.
Are...
|
by: Ily |
last post by:
Hi
I have two applications (both in asp.net 2.0).
I want both to have a common company wide look and feel
How can I do this using master pages?
Also If new applications are added, I want...
|
by: Rahul Babbar |
last post by:
Hi,
I am getting the following error, while executing the simple insert
script on a few tables.
INSERT INTO <table_name>(<col1>) VALUES (1);
DB2 SQL error: SQLCODE: -440, SQLSTATE: 42884,...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |