473,473 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question on macro

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

Mar 30 '07 #1
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/

Mar 30 '07 #2
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.
Mar 30 '07 #3
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
Mar 30 '07 #4

<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?
Mar 30 '07 #5

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

Similar topics

6
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...
1
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...
2
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...
11
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...
5
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...
53
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;
60
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
10
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;...
6
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...
12
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 *' :...
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...
1
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...
0
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...
0
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 ...
0
bsmnconsultancy
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...

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.