473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

usage of ## for macro string catenation

Hello all,

I'm trying to write a macro that will let me access the fields of a
struct variable, given the field name as a parameter. I tried the
follwing which produces the desired result when run thru gnu cpp, but
it complains and stops.

#define ref(var,field) var. ## field

struct mystruct {
int i;
char c;
};

int main (void) {
struct mystruct aaa;
ref(aaa,i) = 1;
}

Any help is greatly appreciated.
___
Regards,
Ziyan Maraikar
Vrije Universiteit, Amsterdam.

Nov 15 '05 #1
5 1766
In article <11*********************@o13g2000cwo.googlegroups. com>,
ziyan <zi****@gmail.com> wrote:
I'm trying to write a macro that will let me access the fields of a
struct variable, given the field name as a parameter. I tried the
follwing which produces the desired result when run thru gnu cpp, but
it complains and stops. #define ref(var,field) var. ## field


The gcc message is along the lines of,

pasting "." and "i" does not give a valid preprocessing token

which is accuate. ## is used to create new tokens, and period is
not a possible character in a preprocessor token.

For your purposes, just use

#define ref(var,field) var.field
--
Studies show that the average reader ignores 106% of all statistics
they see in .signatures.
Nov 15 '05 #2
Hi Walter

Thanx for the tip.
For your purposes, just use

#define ref(var,field) var.field


Hmm...seems obvious enough. Guess I assumed macro replacement only
works on whitespace separated tokens.

BTW I came up with a nice hack for a hashtable I implemented using this
technique:

typedef struct {
enum {INT, FLOAT, STRING} type;
union {
int INT;
float FLOAT;
char *STRING;
} value;
} Hashobject;

typedef struct {
char *key;
Hashobject object;
short chain;
} Hashentry;

#define hash_add(_ht, _key, _value, _type){\
Hashentry _he; \
_he.key = _key; \
_he.object.type = _type; \
_he.object.value._type = _value;\
hash_addentry (_ht, _he); \
}

int hash_addentry (Hashtable *ht, Hashentry ent);

Nov 15 '05 #3
"ziyan" <zi****@gmail.com> writes:
For your purposes, just use

#define ref(var,field) var.field


Hmm...seems obvious enough. Guess I assumed macro replacement only
works on whitespace separated tokens.

[snip]

As far as the compiler is concerned, tokens are tokens, and spaces
between them are often optional. Just as "x + y" and "x+y" are
equivalent, "var . field" is equivalent to "var.field". (As a matter
of style, of course, "var . field" is ugly.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4


Keith Thompson wrote:
"ziyan" <zi****@gmail.com> writes:
For your purposes, just use

#define ref(var,field) var.field


Hmm...seems obvious enough. Guess I assumed macro replacement only
works on whitespace separated tokens.


[snip]

As far as the compiler is concerned, tokens are tokens, and spaces
between them are often optional. Just as "x + y" and "x+y" are
equivalent, "var . field" is equivalent to "var.field". (As a matter
of style, of course, "var . field" is ugly.)


... which raises the question of why anyone would
want to write `ref(var,field)' instead of `var.field'.
Maybe I'm blind, but I don't see the attraction ...

--
Er*********@sun.com

Nov 15 '05 #5
Eric Sosman <er*********@sun.com> writes:
Keith Thompson wrote:
"ziyan" <zi****@gmail.com> writes:
For your purposes, just use

#define ref(var,field) var.field

Hmm...seems obvious enough. Guess I assumed macro replacement only
works on whitespace separated tokens.


[snip]

As far as the compiler is concerned, tokens are tokens, and spaces
between them are often optional. Just as "x + y" and "x+y" are
equivalent, "var . field" is equivalent to "var.field". (As a matter
of style, of course, "var . field" is ugly.)


... which raises the question of why anyone would
want to write `ref(var,field)' instead of `var.field'.
Maybe I'm blind, but I don't see the attraction ...


By itself, it doesn't make much sense, but if ziyan's article upthread
shows a hash table macro that uses it. The macro has several
arguments, and it's not obvious from a call that one of them happens
to be a member name

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #6

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
27
by: TheDD | last post by:
Hello all, right now, i'm using the following macro to automatically add informations to exceptions: #define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args) but AFAIK, it's...
21
by: Eric | last post by:
Hello all, I've got this line of given code (cannot change this; wizard-generated, but value may change someday): #define IDB_BUTTON 225 Somewhere in the code I found /...
32
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
3
by: ooze | last post by:
I have read the Programing language C 6.10.3.2 The # operator Constraints 1 Each # preprocessing token in the replacement list for a function-like macro shall be followed by a parameter as the...
8
by: sathyashrayan | last post by:
/* ** PLURALTX.C - How to print proper plurals ** public domain - original algorithm by Bob Stout */ #include <stdio.h> #define plural_text(n) &"s"
1
by: Thierry Lam | last post by:
Let's say I have the following codes: #define macHOME $HELLOHOME How can I concatenate the above macro into a string, for example I want the following result: char *path =...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.