473,383 Members | 1,859 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,383 software developers and data experts.

Struct member access with macro

Hai,
Several advanced programs access the struct's member like the one following:

#define str(p) (p.data)
struct node
{
unsigned int data;
};
int main(void)
{
struct node p;
p.data = 10;
printf("%d\n",str(p));
return 0;
}

The above is just a basic demo and it is not the only thing around
What is the advantage of doing so?
Nov 13 '05 #1
5 15417
da***********@yahoo.com wrote:
Several advanced programs access the struct's member like the one following: #define str(p) (p.data)
struct node
{
unsigned int data;
};
int main(void)
{
struct node p;
p.data = 10;
printf("%d\n",str(p));
return 0;
} The above is just a basic demo and it is not the only thing around
What is the advantage of doing so?


None I can see at the moment. It just helps obfuscate what the program
is doing. And you should use "%d" with unsigned integers, that's what
"%u" is for. And using "str" is something you should avoid because
names starting with "str" are reserved for the implementation.

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2
In <a3*************************@posting.google.com> da***********@yahoo.com writes:
Hai,
Several advanced programs access the struct's member like the one following:

#define str(p) (p.data)
struct node
{
unsigned int data;
};
int main(void)
{
struct node p;
p.data = 10;
printf("%d\n",str(p));
return 0;
}

The above is just a basic demo and it is not the only thing around
What is the advantage of doing so?


No real advantage in this example: it saves some typing (if str(p) is
used often enough) at the expense of rendering the code less readable.

The real advantage comes when both the macro definition and the struct
definition come from an external header and the struct is used as an
abstract data type. In this case, you can access a bit of information
stored in the struct, without having any other information about the
struct.

A typical real world example is the Unix fileno(), often implemented as
a macro (because the file descriptor number is usually a member of the
FILE struct).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3
da***********@yahoo.com wrote:

Hai,
Several advanced programs access the struct's member like the one following:

#define str(p) (p.data)
struct node
{
unsigned int data;
};
int main(void)
{
struct node p;
p.data = 10;
printf("%d\n",str(p));
return 0;
}

The above is just a basic demo and it is not the only thing around
What is the advantage of doing so?


None, in situations as simple as this sample. However,
there can be a benefit in more complex situations. One I ran
across involved a document formatting system that kept track
of a large number of attributes for each paragraph, each word,
and so on. Some of the attributes were used very infrequently,
and it proved worthwhile not to store all of them explicitly
unless they had non-default values. So the code looked like
(*very* inaccurate paraphrase):

struct paragraph {
int this;
int that;
double the_other;
struct rare_attrs *rare;
};

struct rare_attrs {
enum { WEIRD_TYPE, STRANGE_TYPE } attr_type;
int attr_value;
struct rare_attrs *next;
};

#define THIS(p) (p).this
#define THAT(p) (p).that
#define THE_OTHER(p) (p).the_other
#define WEIRD(p) ((p)->rare ? compute_weird(p) : WEIRD_DEFAULT)
#define STRANGE(p) ((p)->rare ? compute_strange(p) : STRANGE_DEFAULT)

.... and so on. The user of WEIRD and STRANGE didn't need to
know whether these attributes were stored right there in the
paragraph struct or were somewhere in an "exceptionals" list
chained from it. Furthermore, if it later turned out that
WEIRD was in actual practice more common than originally
thought, it could be "promoted" into the paragraph struct with
a simple adjustment of the macro, and none of the code that
used it needed to change.

Another use is to hide irrelevant intermediate names, often
in connection with the use of unions:

union flags {
struct {
unsigned int f1 : 1;
unsigned int f2 : 1;
unsigned int f3 : 1;
} s;
unsigned int all_flags;
};

#define F1(p) (p).s.f1
#define F2(p) (p).s.f2
#define F3(p) (p).s.f3
#define ALL(p) (p).all_flags

The intermediate name `s' is just an encumbrance, and could
be done away with if only C would permit anonymous elements (some
compilers support this as a non-conforming extension to the
language). The macros allow the meaningless name to be hidden --
not much of a gain in this simple illustration, but the value
becomes more apparent as the number of such unions within a
single enclosing struct increases.

(By the way, the correspondence between f1/f2/f3 and the bit
positions in all_flags is implementation-defined, and it could
even be the case that some of the flags lie entirely outside
the all_flags element. This technique relies on non-portable
knowledge about the implementation at hand.)

--
Er*********@sun.com
Nov 13 '05 #4
On 29 Sep 2003 06:03:48 -0700, in comp.lang.c ,
da***********@yahoo.com wrote:
Hai,
Several advanced programs access the struct's member like the one following:

#define str(p) (p.data) ....What is the advantage of doing so?


The only reason for doing this would be if you wanted to hide the
definition of p from the program using it. Its a kind of poor man's
private data. IF you did this, then you'd normally put the definition
of p and the macro in a header.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #5
Je***********@physik.fu-berlin.de wrote:
da***********@yahoo.com wrote:
Several advanced programs access the struct's member like the
one following:
#define str(p) (p.data)
struct node
{
unsigned int data;
};

.... snip ...
And using "str" is something you should avoid because names
starting with "str" are reserved for the implementation.


Only if followed by a lower case letter. This case is OK.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #6

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

Similar topics

0
by: Alex | last post by:
i have a module in Access which opens an existing Excel file and envokes a macro within the Excel file to draw graphs. now i am trying to convert the Excel macro to an Access one so that the...
2
by: hilmilho | last post by:
Hi you, I'm facing a strange problem, and I don't know what to do. Maybe someone could help me understand this? I'm storing IP and Netmask on the struct below. 1st I store IP an print it, its...
10
by: googler | last post by:
Hello, I have a very simple question that I'm a bit confused about right now. Is it OK to allocate memory by malloc() or calloc() for a struct member and then call free() on it? For example, I have...
0
by: Evgeny | last post by:
I'm trying to run an Access Macro from .NET. My code right now looks like this: Access.ApplicationClass oAccess = new Access.ApplicationClass(); oAccess.Visible = true; string path =...
0
by: Urs Vogel | last post by:
Hi when setting the struct member alignment of a mixed code project to 'default', what is the actual alignment? I need to know since we're creating (struct-) data dynamically and pass it to a...
1
by: Sen K via .NET 247 | last post by:
(Type your message here) Hi, Any help is appreciated. I have a situation wherein i need to compact access db after somebusiness logic through VB.NET.I've written a function in VBA tocompact...
4
by: myfavdepo | last post by:
Hi friends, i am having some trouble in my prog. with struct member alignment. I have two different static libraries that i use, each with "struct member alignment" set to 8 bytes. In my...
5
by: petschy | last post by:
Hello All, I confronted recently with the fact that the size of a struct member can't be determined with sizeof(Struct::Member). I'm aware of the alternative methods to do this, however, I'm...
3
Markus
by: Markus | last post by:
Why can I only access a struct member through a pointer to said struct? I thought you were able to access a pointer's members via the dot notation? person mark; person *ptr_mark; ptr_mark->age...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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...
0
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
0
BarryA
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...
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...

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.