Hi all,
Can I write some macro which does something like this offset(s,x),
where s is the type of the structure and x is the name of the field in
the structure. offset has to return the position of the element in the
structure of type s.
For eg., if the structure is declared as struct s {int x, int y}. If
you give offset(s,x) return value should be 0. If you give
offset(s,y), return value should be 2. Is it possible to write such a
macro.
Thanks in advance.
Thanks and Regards,
Aruna 4 3529 ar*******@yahoo.co.in wrote:
Hi all,
Can I write some macro which does something like this offset(s,x),
where s is the type of the structure and x is the name of the field in
the structure. offset has to return the position of the element in the
structure of type s.
For eg., if the structure is declared as struct s {int x, int y}. If
you give offset(s,x) return value should be 0. If you give
offset(s,y), return value should be 2. Is it possible to write such a
macro.
Yes, and in fact it's extremely easy:
#include <stddef.h>
#define offset(s,f) offsetof(s, f)
`offsetof` is provided by C for exactly this purpose, because there
is /no portable way/ of defining it oneself. The insides are magic
and are only guaranteed to work on the implementation that provides
it.
--
Of Sets And Hedgehogs
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/ ar*******@yahoo.co.in wrote:
# Hi all,
#
# Can I write some macro which does something like this offset(s,x),
# where s is the type of the structure and x is the name of the field in
# the structure. offset has to return the position of the element in the
# structure of type s.
It's easy to write a macro that will create an expression whose value
is the field offset; it's unlikely you can make evaluate that in the
preprocessor: it requires information from the context sensitive parse.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Don't say anything. Especially you. ar*******@yahoo.co.in wrote:
Hi all,
Can I write some macro which does something like this offset(s,x),
where s is the type of the structure and x is the name of the field in
the structure. offset has to return the position of the element in the
structure of type s.
Easy!
#include <stddef.h>
#define offset(s,x) offsetof(s,x)
For eg., if the structure is declared as struct s {int x, int y}. If
you give offset(s,x) return value should be 0. If you give
offset(s,y), return value should be 2. Is it possible to write such a
macro.
A different requirement, but also easy!
#include <stddef.h>
#define offset(s,x) (offsetof(s,x) ? 2 : 0)
--
Eric Sosman es*****@acm-dot-org.invalid
<ar*******@yahoo.co.inha scritto nel messaggio
news:11**********************@n59g2000hsh.googlegr oups.com...
For eg., if the structure is declared as struct s {int x, int y}. If
you give offset(s,x) return value should be 0. If you give
offset(s,y), return value should be 2. Is it possible to write such a
macro.
Do you know for certain that sizeof(int) == 2 and that there is no padding? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: James Brown |
last post by:
Hi,
I have the following enum declared:
enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ };
What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs...
|
by: Dan |
last post by:
Could someone please help me with auto importing a series of data
files into an Access table. I tried to follow code given below in a
previous messagebut i'm getting error messages.
Here's my...
|
by: MLH |
last post by:
This is NOT of paramount importance. I'm sure there are others
whose questions are mission critical. Mine is not.
I have always wanted a report that looked like this...
Here is some sample...
|
by: Shawn Odekirk |
last post by:
Some code I have inherited contains a macro like the following:
#define setState(state, newstate) \
(state >= newstate) ? \
(fprintf(stderr, "Illegal...
|
by: Mason |
last post by:
I'm having some problems converting VBA for Word 2000 to code that
VB.Net understands. I recorded a macro in Word to add numbering (a.
b. c.) to my paragraphs. I managed to translate quite a bit...
| |
by: Jeff |
last post by:
In the function below, can size ever be 0 (zero)?
char *clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char *p;
clc_assert_not_null(clc_strdup, s);
size = strlen(s) + 1;
|
by: Mark Healey |
last post by:
I have a function that runs through a two dimensional array several times.
01 x=map->X;
02 do
02 {
04 y=map->Y;
05 do
05 {
06 stuff();
07
|
by: haomiao |
last post by:
I want to implement a common list that can cantain any type of data,
so I declare the list as (briefly)
---------------------------------------
struct list
{
int data_size;
int node_num;...
|
by: Patrick |
last post by:
Hi
I am just playing a little bit around with a simple program to use
pointers and unfortunately I get an segmentation violation.
Basically I have 4 arrays that I contain elements that I wanna...
|
by: j1230xz |
last post by:
Ok now the problem is this.
I have compiled some old code with Visual Studio 2008 (the code was
originally writen in Visual Studio 6) and i have this error:
Error 1 error C2681: 'void *' :...
|
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: 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,...
|
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: 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...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |