Question on macro

A

arunaling

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
 
C

Chris Dollin

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.
 
S

SM Ryan

(e-mail address removed) 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.
 
E

Eric Sosman

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!

> 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)
 
A

Army1987

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Macro 3
Defining Macro in C 0
offsetof() Macro Attempt 45
Help with EXT3 Filesystem work 1
C macro 3
MACRO help 23
Lexical Analysis on C++ 1
Struct Member Variable Problems 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top